Q:

AWS CodeDeploy integration with GitLab

I have set up two EC2 instances in a private subnet behind a NAT.
The instances are both in a AutoScalling group.
I want to integrate CodeDeploy with my repository from GitLab.
All I can think of now is running a script on the BeforeInstall hook of the appspec.yml file.
Is there another way to do this?

A:

GitLab doesn't implements a direct integration with AWS services, but you can work around that. You can do something like what you described, and implement all the installation/distribution/auth logic, but then you aren't really getting much from CodeDeploy. What you should do depends on what you are trying to achieve.
Automatic deployment on push:
You can get automatic deployments on commit to GitLab if you bridge their WebHooks with something that can authenticate to AWS. That might look like:

Web Hook in GitLab that sends a push request to a Jenkins server you control.
The Jenkins server uses the Git plugin to pull the source.
The Jenkins server runs your build and test steps.
The Jenkins server uses the CodeDeploy plugin to upload the build artifacts to S3 and create a deployment.

If you want to have manual deployments, you could do the same as above but manually trigger the Jenkins build.
Deploy manually only:
Do the following when you want to deploy:

Use git to checkout the commit you want to deploy.
Run your build and test locally.
Execute the AWS CLI deploy push command to upload your build artifacts to S3.
Create a deployment in CodeDeploy using the bundle uploaded.

