Using a Codepipeline

Enhancing the Codepipeline to set up the database and the Elastic Beanstalk app

Create a new file buildspec_deploy.yml file. Add the following content:

version: 0.2

phases:
  install:
    runtime-versions:
      java: openjdk8
      docker: 18
    commands:
      - pip install awsebcli
  build:
    commands:
      - echo "Setting up the database and the Elastic Beanstalk app"
      - make database app STAGE=dev
      - echo "Deploying Docker container to the Elastic Beanstalk app"
      - make deploy STAGE=dev

Make sure to commit and push everything to the repository:

git add buildspec_deploy.yml
git commit -m 'Enhance pipeline by setting up the database and the Elastic Beanstalk app'

Then push to your branch

git push ...

Additionally enhance the Pipeline by another Stage. Edit cloudformation/pipeline.yml and change it as follows:

...
Resources:
  ...
  AppPipeline:
    Type: 'AWS::CodePipeline::Pipeline'
    Properties:
      Name: !Ref AWS::StackName
      RoleArn: !GetAtt
        - CodePipelineServiceRole
        - Arn
      Stages:
        ...
        - Name: Deploy
          Actions:
            - Name: Deploy
              InputArtifacts:
                - Name: SourceOutput
              ActionTypeId:
                Category: Build
                Owner: AWS
                Version: 1
                Provider: CodeBuild
              OutputArtifacts:
                - Name: DeployOutput
              Configuration:
                ProjectName: !Ref AppDeploy
  ...
  AppDeploy:
    Type: AWS::CodeBuild::Project
    Properties:
      Name: !Sub '${AWS::StackName}-deploy'
      ServiceRole: !Ref CodeBuildServiceRole
      Artifacts:
        Type: CODEPIPELINE
      Environment:
        ComputeType: BUILD_GENERAL1_SMALL
        Image: aws/codebuild/standard:2.0
        Type: LINUX_CONTAINER
      Source:
        Type: CODEPIPELINE
        BuildSpec: buildspec_deploy.yml

Update the Cloudformation stack with the following command in the ci AWS account

AWS_PROFILE=myorg-ci BITBUCKET_TOKEN=... make pipeline

Log in to the AWS Console, switch to the CI account and trigger your pipeline.