0
votes

Within AWS Lambda is it possible to run cloud formation code?
Basically, I want to watch some ec2 instances using CloudWatch. When the usage(CPU) of the EC2 has crossed a certain threshold for some time, I want a Cloudwatch event raised which would in turn trigger a lamba which would in turn provision few more ec2 instances.

Is this possible?

I know there are other services like Autoscaling groups, ECS etc, but just want to know the possibility of above.

(Any suggestions on what is the best approach to dynamically provision more ec2 instances based on load / cpu usage are welcome)

2
You could just use the CloudFormation API from your lambda function, using the AWS SDK for the programming language you are using. This seems like a problem that could be solved with EC2 Auto-Scaling though. - Mark B
yes, this is possible, you can pretty much provision any resource, including cloudformation stacks, using the SDK in lambda. Should you? Probably not; your use case sounds perfect for an autoscaling group, this is exactly what they were created for, see Paolo's answer - JD D

2 Answers

2
votes

The best approach here would be to use Dynamic scaling for the auto scaling groups. From the docs:

When you configure dynamic scaling, you define how to scale the capacity of your Auto Scaling group in response to changing demand.

https://docs.aws.amazon.com/autoscaling/ec2/userguide/as-scale-based-on-demand.html

1
votes

I haven't done this with Lambdas, but I have seen it done with EC2, and you can access almost any AWS service through the service's API with an SDK on a Lambda. If you look here at the documentation for the Javascript SDK Cloudformation API you will see under "Methods":

createStack(params = {}, callback) ⇒ AWS.Request
and
updateStack(params = {}, callback) ⇒ AWS.Request

This should be a good starting point.