Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Amplify headless environment creation #7486

Open
nkumar15 opened this issue Jun 9, 2021 · 10 comments · Fixed by #7874
Open

Amplify headless environment creation #7486

nkumar15 opened this issue Jun 9, 2021 · 10 comments · Fixed by #7874
Labels
bug Something isn't working hosting Issues tied to hosting category p3 strategic

Comments

@nkumar15
Copy link

nkumar15 commented Jun 9, 2021

Hi

I am trying to create an environment with in cicd pipeline, but it seems amplify env add doesn't support headless approach.

I think another approach is to use amplify init with headless option to create environment dynamically, but if it fails with 'AlreadyExisting" error if environment with same name already exist.

Is there any way I can create env dynamically in non interactive way?

Thanks

@jhockett jhockett added multienv Issues tied to multiple environment feature in the CLI question General question labels Jun 9, 2021
@jhockett
Copy link
Contributor

jhockett commented Jun 9, 2021

Hi @nkumar15,

I think another approach is to use amplify init with headless option to create environment dynamically, but if it fails with 'AlreadyExisting" error if environment with same name already exist.

Environment names have to be unique. Is there a reason you can't dynamically change the passed in envName when using headless init?

https://docs.amplify.aws/cli/usage/headless#--amplify

For example:
amplify init --amplify "{ \"envName\":\"enva\", \"projectName\":\"headlessInitTest\", \"defaultEditor\":\"sublime\" }" --yes
amplify init --amplify "{ \"envName\":\"envb\", \"projectName\":\"headlessInitTest\", \"defaultEditor\":\"sublime\" }" --yes

Running the above commands will give me two environments, enva and envb

@nkumar15
Copy link
Author

nkumar15 commented Jun 10, 2021

HI @jhockett

Thanks for you response.

Yes init can create, but there is a problem I am facing.

  1. Created 2 env named dev and qa using amplify init in a amplify project.
  2. Now did a fresh git clone of repo in other location and in same amplify project headless way initialized a new cicd env by passing app id in params.
    3.. Then I tried add hosting
    But when I try to add hosting it shows message as "You have already connected branches to your Amplify Console app. Please visit the Amplify Console to manage your branches."
  3. and if perform publish it shows to run hosting add command.

Please see below for command details from step 2 onwards

headlessinit.sh contents

AMPLIFY="{\
\"projectName\":\"$PROJECT_NAME\",\
\"envName\":\"$AMPLIFY_ENV_NAME\",\
\"appId\":\"$APP_ID\",\
\"defaultEditor\":\"code\"\
}"

REACTCONFIG="{\
\"SourceDir\":\"src\",\
\"DistributionDir\":\"build\",\
\"BuildCommand\":\"npm run-script build\",\
\"StartCommand\":\"npm run-script start\"\
}"

AWSCLOUDFORMATIONCONFIG="{\
\"configLevel\":\"project\",\
\"useProfile\":true,\
\"profileName\":\"amplify\",\
}"

FRONTEND="{\
\"frontend\":\"javascript\",\
\"framework\":\"react\",\
\"config\":$REACTCONFIG\
}"

PROVIDERS="{\
\"awscloudformation\":$AWSCLOUDFORMATIONCONFIG\
}"

amplify init \
--amplify $AMPLIFY \
--frontend $FRONTEND \
--providers $PROVIDERS \
--yes
neeraj@andromeda:~/codes/sftmp/sfwebclient$ amplify --version
4.52.0

neeraj@andromeda:~/codes/sftmp/sfwebclient$ make amplifyinit
./scripts/headlessinit.sh
Note: It is recommended to run this command from the root of your app directory
The following configuration will be applied:

Project information
| Name: myapp
| Environment: cicd
| Default editor: Visual Studio Code
| App type: javascript
| Javascript framework: react
| Source Directory Path: src
| Distribution Directory Path: build
| Build Command: npm run-script build
| Start Command: npm run-script start

Amplify AppID found: XXXXXXXXX. Amplify App name is: myapp
Adding backend environment cicd to AWS Amplify Console app: XXXXXX      <-- intentionally hiding app id 
⠦ Initializing project in the cloud...

CREATE_IN_PROGRESS amplify-myapp-cicd-90620 AWS::CloudFormation::Stack Thu Jun 10 2021 09:06:32 GMT+0800 (Singapore Standard Time) User Initiated
CREATE_IN_PROGRESS AuthRole                      AWS::IAM::Role             Thu Jun 10 2021 09:06:35 GMT+0800 (Singapore Standard Time)
CREATE_IN_PROGRESS UnauthRole                    AWS::IAM::Role             Thu Jun 10 2021 09:06:35 GMT+0800 (Singapore Standard Time)
CREATE_IN_PROGRESS DeploymentBucket              AWS::S3::Bucket            Thu Jun 10 2021 09:06:36 GMT+0800 (Singapore Standard Time)
CREATE_IN_PROGRESS AuthRole                      AWS::IAM::Role             Thu Jun 10 2021 09:06:36 GMT+0800 (Singapore Standard Time) Resource creation Initiated
CREATE_IN_PROGRESS UnauthRole                    AWS::IAM::Role             Thu Jun 10 2021 09:06:36 GMT+0800 (Singapore Standard Time) Resource creation Initiated
⠏ Initializing project in the cloud...

CREATE_IN_PROGRESS DeploymentBucket AWS::S3::Bucket Thu Jun 10 2021 09:06:37 GMT+0800 (Singapore Standard Time) Resource creation Initiated
⠧ Initializing project in the cloud...

CREATE_COMPLETE AuthRole                      AWS::IAM::Role             Thu Jun 10 2021 09:06:58 GMT+0800 (Singapore Standard Time)
CREATE_COMPLETE UnauthRole                    AWS::IAM::Role             Thu Jun 10 2021 09:06:58 GMT+0800 (Singapore Standard Time)
CREATE_COMPLETE DeploymentBucket              AWS::S3::Bucket            Thu Jun 10 2021 09:06:59 GMT+0800 (Singapore Standard Time)
CREATE_COMPLETE amplify-myapp-cicd-90620 AWS::CloudFormation::Stack Thu Jun 10 2021 09:07:01 GMT+0800 (Singapore Standard Time)
✔ Successfully created initial AWS cloud resources for deployments.
✔ Initialized provider successfully.
Initialized your environment successfully.

Your project has been successfully initialized and connected to the cloud!

Some next steps:
"amplify status" will show you what you've added already and if it's locally configured or deployed
"amplify add <category>" will allow you to add features like user login or a backend API
"amplify push" will build all your local backend resources and provision it in the cloud
"amplify console" to open the Amplify Console and view your project status
"amplify publish" will build all your local backend and frontend resources (if you have hosting category added) and provision it in the cloud

Pro tip:
Try "amplify add api" to create a backend API and then "amplify publish" to deploy everything




neeraj@andromeda:~/codes/sftmp/sfwebclient$ amplify push
✔ Successfully pulled backend environment cicd from the cloud.

Current Environment: cicd

| Category | Resource name | Operation | Provider plugin |
| -------- | ------------- | --------- | --------------- |

No changes detected
neeraj@andromeda:~/codes/sftmp/sfwebclient$ amplify hosting add
? Select the plugin module to execute Hosting with Amplify Console (Managed hosting with custom domains, Continuous deployment)
You have already connected branches to your Amplify Console app. Please visit the Amplify Console to manage your branches.

neeraj@andromeda:~/codes/sftmp/sfwebclient$ amplify publish

Please add hosting to your project before publishing your project
Command: amplify hosting add

`AMPLIFY="{\
\"projectName\":\"$PROJECT_NAME\",\
\"envName\":\"$AMPLIFY_ENV_NAME\",\
\"appId\":\"$APP_ID\",\
\"defaultEditor\":\"code\"\
}"

neeraj@andromeda:~/codes/sftmp/sfwebclient$ make amplifyinit
./scripts/headlessinit.sh
Note: It is recommended to run this command from the root of your app directory
The following configuration will be applied:

Project information
| Name: myapp
| Environment: cicd
| Default editor: Visual Studio Code
| App type: javascript
| Javascript framework: react
| Source Directory Path: src
| Distribution Directory Path: build
| Build Command: npm run-script build
| Start Command: npm run-script start

Amplify AppID found: XXXXXXXXX. Amplify App name is: myapp
Adding backend environment cicd to AWS Amplify Console app: XXXXXX      <-- intentionally hiding app id 
⠦ Initializing project in the cloud...

CREATE_IN_PROGRESS amplify-myapp-cicd-90620 AWS::CloudFormation::Stack Thu Jun 10 2021 09:06:32 GMT+0800 (Singapore Standard Time) User Initiated
CREATE_IN_PROGRESS AuthRole                      AWS::IAM::Role             Thu Jun 10 2021 09:06:35 GMT+0800 (Singapore Standard Time)
CREATE_IN_PROGRESS UnauthRole                    AWS::IAM::Role             Thu Jun 10 2021 09:06:35 GMT+0800 (Singapore Standard Time)
CREATE_IN_PROGRESS DeploymentBucket              AWS::S3::Bucket            Thu Jun 10 2021 09:06:36 GMT+0800 (Singapore Standard Time)
CREATE_IN_PROGRESS AuthRole                      AWS::IAM::Role             Thu Jun 10 2021 09:06:36 GMT+0800 (Singapore Standard Time) Resource creation Initiated
CREATE_IN_PROGRESS UnauthRole                    AWS::IAM::Role             Thu Jun 10 2021 09:06:36 GMT+0800 (Singapore Standard Time) Resource creation Initiated
⠏ Initializing project in the cloud...

CREATE_IN_PROGRESS DeploymentBucket AWS::S3::Bucket Thu Jun 10 2021 09:06:37 GMT+0800 (Singapore Standard Time) Resource creation Initiated
⠧ Initializing project in the cloud...

CREATE_COMPLETE AuthRole                      AWS::IAM::Role             Thu Jun 10 2021 09:06:58 GMT+0800 (Singapore Standard Time)
CREATE_COMPLETE UnauthRole                    AWS::IAM::Role             Thu Jun 10 2021 09:06:58 GMT+0800 (Singapore Standard Time)
CREATE_COMPLETE DeploymentBucket              AWS::S3::Bucket            Thu Jun 10 2021 09:06:59 GMT+0800 (Singapore Standard Time)
CREATE_COMPLETE amplify-myapp-cicd-90620 AWS::CloudFormation::Stack Thu Jun 10 2021 09:07:01 GMT+0800 (Singapore Standard Time)
✔ Successfully created initial AWS cloud resources for deployments.
✔ Initialized provider successfully.
Initialized your environment successfully.

Your project has been successfully initialized and connected to the cloud!

Some next steps:
"amplify status" will show you what you've added already and if it's locally configured or deployed
"amplify add <category>" will allow you to add features like user login or a backend API
"amplify push" will build all your local backend resources and provision it in the cloud
"amplify console" to open the Amplify Console and view your project status
"amplify publish" will build all your local backend and frontend resources (if you have hosting category added) and provision it in the cloud

Pro tip:
Try "amplify add api" to create a backend API and then "amplify publish" to deploy everything




neeraj@andromeda:~/codes/sftmp/sfwebclient$ amplify push
✔ Successfully pulled backend environment cicd from the cloud.

Current Environment: cicd

| Category | Resource name | Operation | Provider plugin |
| -------- | ------------- | --------- | --------------- |

No changes detected
neeraj@andromeda:~/codes/sftmp/sfwebclient$ amplify hosting add
? Select the plugin module to execute Hosting with Amplify Console (Managed hosting with custom domains, Continuous deployment)                                                                                    
**You have already connected branches to your Amplify Console app. Please visit the Amplify Console to manage your branches.** <----------issue here

neeraj@andromeda:~/codes/sftmp/sfwebclient$ amplify publish

Please add hosting to your project before publishing your project
**Command: amplify hosting add**                                                       <----------issue here

How do I solve this issue?

@jhockett
Copy link
Contributor

How do I solve this issue?

@nkumar15 it depends on what you want your hosting setup to look like. Check out our docs on the different hosting options available: https://docs.amplify.aws/cli/hosting

It does look like we can improve amplify publish to be smarter about printing the message:
"Please add hosting to your project before publishing your project
Command: amplify hosting add"

It should display something more helpful when you've connected your git branches in Amplify Console. Marking this issue as a bug.

@jhockett jhockett added bug Something isn't working hosting Issues tied to hosting category and removed question General question multienv Issues tied to multiple environment feature in the CLI labels Jun 12, 2021
Bentheburrito added a commit to MLH-Fellowship/amplify-cli that referenced this issue Aug 3, 2021
@renebrandel
Copy link
Contributor

I think we should support amplify env add <env_name> -y the "-y" will pick the current AWS Profile/Admin UI config to create the new environment.

@bombillazo
Copy link

bombillazo commented Aug 13, 2021

I think we should support amplify env add <env_name> -y the "-y" will pick the current AWS Profile/Admin UI config to create the new environment.

This would be great. Right now I have a custom CI/CD process where i need to deploy backend and front-end (a mobile app) separately. For my FE, I need to checkout the repo code which does not have committed config files like aws-exports.js so im needing to install amplify, init the env and pull my deploy env in the CI/CD runner simply to generate that file since there is no command to do so.

Another great command would be amplify env generateAWSExports or something that generates that file without needing to pull the whole environment.

cjihrig pushed a commit that referenced this issue Aug 25, 2021
…red in Amplify Console (#7874)

* fix(cli): add specific check for cd branches when running `amplify publish`

fix #7486

* test(cli): add tests for hasCdBranches

* refactor(cli): mockBranches explicit values set per-test
jhockett pushed a commit that referenced this issue Aug 25, 2021
…red in Amplify Console (#7874)

* fix(cli): add specific check for cd branches when running `amplify publish`

fix #7486

* test(cli): add tests for hasCdBranches

* refactor(cli): mockBranches explicit values set per-test
@github-actions github-actions bot added the referenced-in-release Issues referenced in a published release changelog label Sep 2, 2021
@github-actions
Copy link

github-actions bot commented Sep 2, 2021

👋 Hi, this issue was referenced in the v5.4.0 release!

Check out the release notes here https://github.com/aws-amplify/amplify-cli/releases/tag/v5.4.0.

@cjihrig
Copy link
Contributor

cjihrig commented Sep 7, 2021

Reopening this issue, as the fix was reverted.

@cjihrig cjihrig reopened this Sep 7, 2021
marcvberg pushed a commit to marcvberg/amplify-cli that referenced this issue Oct 13, 2021
…red in Amplify Console (aws-amplify#7874)

* fix(cli): add specific check for cd branches when running `amplify publish`

fix aws-amplify#7486

* test(cli): add tests for hasCdBranches

* refactor(cli): mockBranches explicit values set per-test
@josefaidt josefaidt added the p3 label Mar 9, 2022
@josefaidt
Copy link
Contributor

Marking as a p3 but we should look at this as a part of the headless refactor for consistency

@josefaidt josefaidt removed the referenced-in-release Issues referenced in a published release changelog label Feb 28, 2023
@andrewclam1991
Copy link

I have an use case in a CI (ex. Gitlab) pipeline to integrate merge request changes for testing. something like following would be great!

AMPLIFY_PROVIDERS: '{
  "useProfile":false,
  "accessKeyId":"$AMPLIFY_ACCESS_KEY_ID",
  "secretAccessKey":"$AMPLIFY_SECRET_ACCESS_KEY"
}'

amplify env add $CI_COMMIT_SHORT_SHA \
      --providers $AMPLIFY_PROVIDERS \
      --yes

$CI_COMMIT_SHORT_SHA , or other short unique id.

@armenr
Copy link

armenr commented Jan 26, 2024

I just dropped in to see what condition this condition is in? :)

We've got a bad need for this too...

I even tried this:

AWS_SECRET_ACCESS_KEY=$(aws configure get aws_secret_access_key --profile my_amplify_thingy) AWS_ACCESS_KEY_ID=$(aws configure get aws_access_key_id --profile my_amplify_thingy) amplify env add --envName testingit --yes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working hosting Issues tied to hosting category p3 strategic
Projects
None yet
9 participants