aws / aws-sam-cli

CLI tool to build, test, debug, and deploy Serverless applications using AWS SAM
https://aws.amazon.com/serverless/sam/
Apache License 2.0
6.51k stars 1.17k forks source link

Bug: SAM deploy command with `sam pipeline init` failed to persist CodePipeline #3907

Closed Stewart86 closed 2 years ago

Stewart86 commented 2 years ago

Description:

Following the guide in AWS SAM documentation, step by step created pipeline as instructed in the documentation till the final step where I copied the command from step 4 to connect to CodeCommit, I ran;

sam deploy -t codepipeline.yaml --stack-name prod --capabilities=CAPABILITY_IAM 

I can see that CloudFormation events being generated in the shell (with Successfully created/updated stack - prod in None), as well as seeing the CodePipeline being generated and running the deployment stages.

However, as soon as the deployment is done, that pipeline is missing from AWS Developer Tool Console.

Shouldn't the pipeline be retained and when a new commit to the branch is merged, it automatically run the pipeline every time? why is my pipeline got removed right after the deployment is done?

Steps to reproduce:

# pick python-sample on init
sam init

sam pipeline bootstrap

sam pipeline init

# push change to CodeCommit
git add .
git commit -am "deploy"
hit push

sam deploy -t codepipeline.yaml --stack-name prod --capabilities=CAPABILITY_IAM 

Observed result:

in AWS CodePipeline console, pipeline is removed after a while. Resources seems to be deployed.

Expected result:

in AWS CodePipeline console, pipeline should remain, and when the next branch merge, pipeline will run to update resources.

Additional environment details (Ex: Windows, Mac, Amazon Linux etc)

  1. OS: Windows 11 running WSL 2
  2. sam --version: 1.50.0
  3. AWS region: ap-southeast-1

Add --debug flag to command you are running

Stewart86 commented 2 years ago

After trying to understand the whole logic of the deploy command and their internals, I have managed to retain the CodePipeline in AWS console to run for my automated CI / CD pipeline.

Just to put it out there so if anyone facing the similar issue, can refer to this. Also, suggest an update on the guide to reflect.

Here is what i found.

  1. sam pipeline init

    • define the two stages stack-name that you be use when sam deploy is executed
    • in this case, i i named it "prod" and "stage"
    • the stack is not created yet, it will be created after the execution of sam deploy
  2. sam deploy -t codepipeline.yaml

    • generate two CloudFormation stack name for the two stages you defined in sam pipeline init
    • generate a CloudFormation stack name you defined in sam deploy but this time with template.yaml
    • in this case, i defined "prod"
    • therefore, then sam deploy is called, it finds a CloudFormation stack that you indicate and modify
  3. when sam deploy with codepipeline.yaml template, a CloudFormation stack is created on the fly from your terminal to create the AWS CodePipeline, AWS CodeBuild and any other required resources for CI / CD with the stack name you defined in sam deploy

  4. once it is successfully created/updated, the CodePipeline will run, which includes a creation / updating of the CloudFormation stack you defined during sam pipeline init

  5. however at this step, if you already have a CloudFormation stack with the same name, it will be modified.

  6. In this case, it modified the stack I created for the CI / CD, but CodePipeline is mostly only deleted last, therefore it is removed as soon as it's done with the deployment

The moral of this story is, do not name your stack for sam pipeline init and sam deploy the same!