Created
January 18, 2020 21:52
-
-
Save hendrixroa/d9247557dd75b7930c7b2025112c5bf5 to your computer and use it in GitHub Desktop.
Codedeploy script to trigger deployment when an AWS ECS service need to be updated
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import AWS = require('aws-sdk'); | |
import crypto = require('crypto'); | |
import minimist = require('minimist'); | |
const args: any = minimist(process.argv); | |
const codedeploy = new AWS.CodeDeploy({ | |
region: process.env.AWS_DEFAULT_REGION || 'us-east-2', | |
}); | |
export class Deploy { | |
public async createDeployment() { | |
const appspec: any = { | |
Resources: [ | |
{ | |
TargetService: { | |
Properties: { | |
LoadBalancerInfo: { | |
ContainerName: args.service, | |
ContainerPort: Number(args.port), | |
}, | |
NetworkConfiguration: { | |
AwsvpcConfiguration: { | |
AssignPublicIp: args.public_ip, | |
SecurityGroups: [args.sg], | |
Subnets: JSON.parse(args.subnets), | |
}, | |
}, | |
PlatformVersion: 'LATEST', | |
TaskDefinition: args.task, | |
}, | |
Type: 'AWS::ECS::Service', | |
}, | |
}, | |
], | |
version: 1, | |
}; | |
if (args.traffic === 'false') { | |
delete appspec.Resources[0].TargetService.Properties.LoadBalancerInfo; | |
} | |
const appspecStr = JSON.stringify(appspec); | |
return new Promise((resolve, reject) => { | |
const paramsDeployment = { | |
applicationName: `Deployment-${args.service}`, | |
autoRollbackConfiguration: { | |
enabled: false, | |
}, | |
deploymentGroupName: `deployment-group-${args.service}`, | |
revision: { | |
appSpecContent: { | |
content: appspecStr, | |
sha256: crypto | |
.createHash('sha256') | |
.update(appspecStr) | |
.digest('hex'), | |
}, | |
revisionType: 'AppSpecContent', | |
}, | |
}; | |
codedeploy.createDeployment(paramsDeployment, (err: any, data: any) => { | |
if (err) { | |
reject(err); | |
} | |
resolve(data); | |
}); | |
}); | |
} | |
} | |
const deploy: Deploy = new Deploy(); | |
deploy | |
.createDeployment() | |
.then((data: any) => { | |
// tslint:disable-next-line: no-console | |
console.log('Updated the service: ', data); | |
}) | |
.catch((err: any) => { | |
// tslint:disable-next-line: no-console | |
console.error('error updating service: ', err); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment