Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to do CI/CD with AWS CDK (python) using GitLab CI?

I am using AWS CDK (with Python) for a containerized application that runs on Fargate. I would like to run cdk deploy in a GitLab CI process and pass the git tag as an environment variable that replaces the container running in Fargate. I am currently doing something similar with CloudFormation (aws cloudformation update-stack ...). Is anyone else doing CI/CD with AWS CDK in this way? Is there a better way to do it?

Also, what should I use for my base image for this job? I was thinking that I can either start with a python container and install node or vice versa. Or maybe there is prebuilt container somewhere that I haven't been able to find yet.

Here is start that seems to be working well:

CDK:
  image: python:3.8
  stage: deploy
  before_script:
    - apt-get -qq update && apt-get -y install nodejs npm
    - node -v
    - npm i -g aws-cdk
    - cd awscdk
    - pip3 install -r requirements.txt
  script:
    - cdk diff
    - cdk deploy --require-approval never

Edit 2020-05-04:

CDK can build docker images during cdk deploy, but it needs access to docker. If you don't need docker, the above CI job definition should be fine. Here's the current CI job I'm using:

cdk deploy:
  image: docker:19.03.1
  services:
    - docker:19.03.5-dind
  stage: deploy
  only:
    - master
  before_script:
    - apk add --no-cache python3
    - python3 -V
    - pip3 -V
    - apk add nodejs-current npm
    - node -v
    - npm i -g aws-cdk
    - cd awscdk
    - pip3 install -r requirements.txt
  script:
    - cdk bootstrap aws://$AWS_ACCOUNT_ID/$AWS_DEFAULT_REGION
    - cdk deploy --require-approval never

The cdk bootstrap is needed because I am using assets in my cdk code:

        self.backend_task.add_container(
            "DjangoBackend",
            image=ecs.AssetImage(
                "../backend",
                file="scripts/prod/Dockerfile",
                target="production",
            ),
            logging=ecs.LogDrivers.aws_logs(stream_prefix="Backend"),
            environment=environment_variables,
            command=["/start_prod.sh"],
        )

Here's more information on cdk bootstrap: https://github.com/aws/aws-cdk/blob/master/design/cdk-bootstrap.md

like image 433
briancaffey Avatar asked Apr 29 '20 02:04

briancaffey


People also ask

Is GitLab good for CI CD?

Due to its great issue tracking and issue shuffling features, GitLab is a preferred CI/CD tool for numerous open-source projects. It smartly allows you to parallel test pull requests and branches. For simple and trouble-free monitoring, the testing outcomes are displayed on the GitLab UI.

How CI CD pipeline works in GitLab?

Source code control: Host code on GitLab to integrate your app with major software and services. CI/CD: Use GitLab CI/CD to commit all code, build and run the required tests. Deploy code to UAT: Configure GitLab CI/CD to deploy code to the UAT server. Deploy to production: Repeat the CI/CD step to deploy code to UAT.


1 Answers

you definitely have to use CDK deploy inside the CI/CD pipeline if you have lambda or ECS assets, otherwise, you could run CDK synth and pass the resulting Cloudformation to AWS Code Deploy. That means a lot of your CI/CD will be spent deploying which might drain your free tier build minutes or just means you pay more (AWS Code Deploy is free)

I do something similar with Golang in CircleCi. I use the Go base image and install nodejs and cdk. I use this base image to build all my go binaries, the vuejs frontend and compile cdk typescript and deploy it.

FROM golang:1.13

RUN go get -u -d github.com/magefile/mage
WORKDIR $GOPATH/src/github.com/magefile/mage
RUN go run bootstrap.go

RUN curl -sL https://deb.nodesource.com/setup_12.x | bash -
RUN apt-get install -y nodejs
RUN npm i -g [email protected]
RUN npm i -g typescript
RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -
RUN echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list
RUN apt update && apt install yarn

I hope that helps.

like image 163
Jonny Rimek Avatar answered Nov 16 '22 02:11

Jonny Rimek