Skip to content

Instantly share code, notes, and snippets.

@vovanmix
Last active July 20, 2016 21:22
Show Gist options
  • Save vovanmix/f3d1980919f2ac2c3da9f56e824c3f58 to your computer and use it in GitHub Desktop.
Save vovanmix/f3d1980919f2ac2c3da9f56e824c3f58 to your computer and use it in GitHub Desktop.
AWS code deploy and auto scaling

#Auto Scaling ##1. Have a config (ex. .env) file in a private S3 ##2. Create a reference instance ##3. Install AWS cli app to the server ##4. Create a shell script to download config file ##5. Create an image with all stack software installed, with shell script in place Write bash scripts in the AMI creating interface to automatically bootstrap the instance on create

#Code Deploy configuration ##Create an App ##Create an Instance Profile ###Create a policy http://docs.aws.amazon.com/codedeploy/latest/userguide/how-to-create-service-role.html#how-to-create-service-role-console http://docs.aws.amazon.com/codedeploy/latest/userguide/how-to-create-iam-instance-profile.html

IAM -> create policy -> your own policy -> CodeDeploy-EC2-Permissions

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Action": [
        "s3:Get*",
        "s3:List*"
      ],
      "Effect": "Allow",
      "Resource": "*"
    }
  ]
}

###Create a role and attach a polity to it IAM -> create Role CodeDeploy-EC2

AWS Service Roles / AWS EC2 / check CodeDeploy-EC2-Permissions and CloudWatchLogsFullAccess

##Create a Service Role IAM -> create Role CodeDeployServiceRole

AWS Service Roles / AWS CodeDeploy / check all

##Set up instance Select IAM Role CodeDeploy-EC2

In setting up instance, paste this under Advanced Details:

#!/bin/bash
yum -y update
yum install -y ruby
yum install -y aws-cli
mkdir -p /tmp/codedeploy
cd /tmp/codedeploy
aws s3 cp s3://aws-codedeploy-us-west-2/latest/install . --region us-west-2 
chmod +x ./install
./install auto

##Set up appspec.yml hooks: http://docs.aws.amazon.com/codedeploy/latest/userguide/app-spec-ref-hooks.html

##Create deployment ###Authorize Github Just click the button

Paste the repo name starting with your username

##Integrate with github: ###Generate token repo section checked

https://github.com/settings/tokens ###add to composer.json

    "config": {
        "preferred-install": "dist",
        "github-oauth": {
            "github.com": "<token here>"
        }
    },

Another option: copy json with a token to ~/.composer/config.json in a deploy hook

http://blog.simplytestable.com/creating-and-using-a-github-oauth-token-with-travis-and-composer/

##Troubleshooting ###ApplicationStop fails If the script in ApplicationStop fails, it will never be updated from the repo. Need to clear the cache:

sudo service codedeploy-agent stop
sudo rm -R /opt/codedeploy-agent/deployment-root/*
sudo service codedeploy-agent start

after that, remove all previously deployed files

###File already exists at location remove all previously deployed files or set overwrite: yes flug under - source ###Duplicate permission setting instructions use except: [file, dir] to avoid overlapping

#Code Pipeline configuration

##Thoughts

All scripts that should rub before - after a deployment, like stop server, composer install, copy config, run server etc, can be put into sh files inside the repo and run using appspec.yml hooks

Have 2 deploy apps, one for staging and another for prod. Bind them together with CodePipeline with an approval step in between and SNS notifications before and after approval

All operations with deployment can be run from the local terminal (configured AWS CLI), without visiting a console

Can store the apache/nginx/php config in git, and deploy it as a part of deployment. It's a good alternative for Ansible/Chef if we don't plan to have lots of maintenance, but apache/nginx/php configs can change once in a while

[?] What to do with credentials and settings? Also store in git? Or at S3 and have a separate deploy task just for config?

[?] How to automatically run tests using CI before deploy?

[?] How to build JS, upload it to S3 and clear the CDN cache with CI? Probably we can get fingerprints on this stage and pass them to the backend config while deploying backend?

#Other https://www.youtube.com/watch?v=qZa5JXmsWZs

https://blogs.aws.amazon.com/application-management/post/Tx33XKAKURCCW83/Automatically-Deploy-from-GitHub-Using-AWS-CodeDeploy

##Bitbucket https://aws.amazon.com/blogs/apn/announcing-atlassian-bitbucket-support-for-aws-codedeploy/

##Travis https://docs.travis-ci.com/user/deployment/codedeploy

#Config Have a appspec file with configuration.

Example:

os: linux
files:
  - source: Config/config.txt
    destination: webapps/Config
  - source: source
    destination: /webapps/myApp
hooks:
  BeforeInstall:
    - location: Scripts/UnzipResourceBundle.sh
    - location: Scripts/UnzipDataBundle.sh
  AfterInstall:
    - location: Scripts/RunResourceTests.sh
      timeout: 180
  ApplicationStart:
    - location: Scripts/RunFunctionalTests.sh
      timeout: 3600
  ValidateService:
    - location: Scripts/MonitorService.sh
      timeout: 3600
      runas: codedeployuser

https://github.com/tqc/codedeploy-scripts/tree/master/src

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment