Skip to content

Instantly share code, notes, and snippets.

@guillaumesmo
Last active June 21, 2020 18:31
Show Gist options
  • Save guillaumesmo/231b8637e1b5583646fe982b09bb433e to your computer and use it in GitHub Desktop.
Save guillaumesmo/231b8637e1b5583646fe982b09bb433e to your computer and use it in GitHub Desktop.
Resources:
CustomResourceRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service: 'lambda.amazonaws.com'
Action: 'sts:AssumeRole'
Policies:
- PolicyName: 'customresource'
PolicyDocument:
Statement:
- Effect: Allow
Action:
- 'autoscaling:DescribeAutoScalingGroups'
Resource: '*'
- Effect: Allow
Action:
- 'logs:CreateLogGroup'
- 'logs:CreateLogStream'
- 'logs:PutLogEvents'
Resource: '*'
AutoScalingGroupARNFunction:
Type: AWS::Lambda::Function
Properties:
Code:
ZipFile: |
const aws = require('aws-sdk')
const response = require('cfn-response')
const autoscaling = new aws.AutoScaling({apiVersion: '2011-01-01'})
const ecs = new aws.ECS({apiVersion: '2014-11-13'})
exports.handler = function(event, context) {
console.log(`AWS SDK Version: ${aws.VERSION}`)
console.log(`Request received: ${JSON.stringify(event)}`)
if (event.RequestType === 'Create') {
autoscaling.describeAutoScalingGroups({AutoScalingGroupNames: [event.ResourceProperties.ASGName]})
.promise()
.then(asgResponse => {
console.log(`ASG found: ${asgResponse.AutoScalingGroups[0].AutoScalingGroupARN}`)
response.send(event, context, response.SUCCESS, {}, asgResponse.AutoScalingGroups[0].AutoScalingGroupARN)
})
.catch(err => {
console.error(err)
response.send(event, context, response.FAILED)
})
} else {
response.send(event, context, response.SUCCESS)
}
}
Handler: 'index.handler'
MemorySize: 128
Role: !GetAtt 'CustomResourceRole.Arn'
Runtime: 'nodejs12.x'
Timeout: 30
AutoScalingGroupARN:
Type: Custom::AutoScalingGroupARN
Properties:
ServiceToken: !GetAtt AutoScalingGroupARNFunction.Arn
ASGName: !Ref AsgEcsHosts
EcsCapacityProvider:
Type: AWS::ECS::CapacityProvider
Properties:
AutoScalingGroupProvider:
AutoScalingGroupArn: !Ref AutoScalingGroupARN
ManagedScaling:
Status: ENABLED
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment