Deploy stage

The Deploy stage is where your CDK application and all its resources are created an in an AWS account. The most common way to do this is by using CloudFormation ChangeSets to deploy. This means that this stage will have action: the CreateUpdateStackAction.

Add the Deploy stage to your pipeline_stack.py:

## Deploy stage
pipeline.add_stage(
    stage_name='Deploy',
    actions=[
        aws_codepipeline_actions.CloudFormationCreateUpdateStackAction(
            action_name='Deploy_Stack',
            template_path=cdk_build_output.at_path("sls-app.template.json"),
            stack_name='sls-app',
            admin_permissions=True,
            parameter_overrides=dict(
                lambda_code.assign(
                    bucket_name=lambda_build_output.s3_location.bucket_name,
                    object_key=lambda_build_output.s3_location.object_key,
                    object_version=lambda_build_output.s3_location.object_version
                )
            ),
            extra_inputs=[lambda_build_output],
            run_order=1
        )
    ]
)
✍️ Full pipeline_stack.py

Deploy the pipeline

cdk deploy PipelineStack

Push code changes

After deploy PipelineStack successfully, just push our code changes.

cd ~/environment/sls-app
git add .
git commit -m "Add pipeline"
git push

Verify pipeline succeeds

Navigate to your CodePipeline again, and wait for it to trigger automatically. This time the deployment will succeed. Pipeline success Congratulations! You have created a CI/CD pipeline for a Serverless application with CDK! 👏
And from now on, you just need to update the codes and push to your repository, the pipeline will build and deploy your stack, no need to cdk deploy unless you want to update the PipelineStack stack 😅.