How to deploy to AWS Beanstalk with GitLab CI

Viewed 5045

How To Deploy a Node App on AWS Elastic Beanstalk, Docker, and Gitlab ci.

I've created a simple node application. Dockerized the node application.

What I'm trying to do is deploy my application using gitlab ci.

This is what I have so far:

image: docker:git
services:
  - docker:dind

stages:
  - build
  - release
  - release-prod

variables:
  CI_REGISTRY: registry.gitlab.com
  CONTAINER_TEST_IMAGE: registry.gitlab.com/testapp/routing:$CI_COMMIT_REF_NAME
  CONTAINER_RELEASE_IMAGE: registry.gitlab.com/testapp/routing:latest

before_script:
  - echo "$CI_REGISTRY_PASSWORD" | docker login -u "$CI_REGISTRY_USER" --password-stdin "$CI_REGISTRY"

build:
  stage: build
  script:
    - docker build -t $CONTAINER_TEST_IMAGE -f Dockerfile.prod .
    - docker push $CONTAINER_TEST_IMAGE

release-image:
  stage: release
  script:
    - docker pull $CONTAINER_TEST_IMAGE
    - docker tag $CONTAINER_TEST_IMAGE $CONTAINER_RELEASE_IMAGE
    - docker push $CONTAINER_RELEASE_IMAGE
  only:
    - master

release-prod:
  stage: release-prod
  script:

  when: manual

I'm stuck on release-prod stage. I'm just not sure how I can deploy the app to AWS Beanstalk.

Since I have the docker images have been created and stored in gitlab registry. All I want to do is instruct AWS Beanstalk to download the docker images from gitlab registry and are start the application.

I also have a Dockerrun.aws.json which defines the services.

2 Answers

Your Dockerrun.aws.json file is what Beanstalk uses as the final say in what is deployed.

The option I found to work for us was to make a custom docker image with the eb cli installed so we can run eb deploy... from the gitlab-ci.yml file.

This requires AWS permissions for the runner to be able to access the aws service though so a user or permissions come into play. But they would any way it's setup.

GitLab project - CI/CD settings aws user keys (Ideally it's set up to use an IAM role instead but User/keys will work - I'm not too familiar with getting temporary access which might be the best thing for this but again, I'm not sure how that works)

We use a custom EC2 instance as our runner to run the pipeline so I'm not sure about shared runners - we had a concern of passing aws user creds to a shared runner pipeline...

build stage: build and push the docker image to our ECR repository or your use case

deploy stage: have a custom image stored in GitLab that has pre installed the eb cli. Then run eb deploy env-name

This is the dockerfile we use for our PHP project. Some of the installs aren't necessary for your case... This could also be improved by adding a USER and package versions. This will create a docker image that has the eb cli installed though.

FROM node:12

RUN apt-get update && apt-get -y --allow-unauthenticated install apt-transport-https ca-certificates curl gnupg2 software-properties-common ruby-full \
    && add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/debian $(lsb_release -cs) stable"
RUN apt-get update && apt-get -y --allow-unauthenticated install docker-ce \
    && apt-get -y install build-essential zlib1g-dev libssl-dev libncurses-dev libffi-dev libsqlite3-dev libreadline-dev libbz2-dev python-pip python3-pip
RUN git clone https://github.com/aws/aws-elastic-beanstalk-cli-setup.git \
    && ./aws-elastic-beanstalk-cli-setup/scripts/bundled_installer
RUN python3 --version && apt-get update && apt-get -y install python3-pip \
    && pip3 install awscli boto3 botocore && pip3 install boto3 botocore --upgrade

Example gitlab-ci.yml setup

release-prod:
  image: registry.gitlab.com/your-acct/project/custom-image
  stage: release-prod
  script:
    - service docker start
    - echo 'export PATH="/root/.ebcli-virtual-env/executables:$PATH"' >> ~/.bash_profile && source ~/.bash_profile
    - echo 'export PATH=/root/.pyenv/versions/3.7.2/bin:$PATH' >> /root/.bash_profile && source /root/.bash_profile
    - eb deploy your-environment
  when: manual

you could also add the echo commands to the custom gitlab image also so all you need to run is eb deploy...

Hope this helps a little

Related