aws-amplify / amplify-cli

The AWS Amplify CLI is a toolchain for simplifying serverless web and mobile development.
Apache License 2.0
2.81k stars 821 forks source link

Amplify headless environment creation #7486

Open nkumar15 opened 3 years ago

nkumar15 commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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.

renebrandel commented 3 years ago

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 commented 3 years ago

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.

github-actions[bot] commented 3 years ago

👋 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 commented 3 years ago

Reopening this issue, as the fix was reverted.

josefaidt commented 2 years ago

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

andrewclam1991 commented 10 months ago

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 commented 9 months ago

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