Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS: Use EC2 instance creation to trigger lambda

I am compiling a fairly complex CloudFormation template and at some point I am creating an ec2 instance;

I want to create a lambda function that:

  • takes as input parameter the public IP of the instance created in this CF stack
  • opens a security group port for that particular IP (the security group is not part of the specific CF template and it belongs to a different region).

Is this possible?

Ι am asking because (among others) ec2 is not listed as a potential lambda trigger in the console and wanted to see whether there is a simpler way around this than posting details about the creation in an sns or sqs which then in turn triggers the lambda.

like image 665
pkaramol Avatar asked Jul 03 '26 04:07

pkaramol


1 Answers

There are two options that I can think of:

  1. As user @krishna_mee2004 stated, you can use CloudWatch to listen on your EC2 instance and this in turn will trigger your lambda.

  2. On your EC2 instance, there is a field called User data under the Instance Details. In User data you can add commands that should be ran whenever your EC2 instance is deployed. From here you can invoke your lambda.

Here is documentation on EC2 user data. Here is documentation on invoking your lambda from the CLI.

Personally, I'd recommend option 1 because I prefer using AWS tools whenever I get the chance and CloudWatch is a perfect example of this. However, option 2 might give you more control over what payload is sent to the lambda.

like image 74
Gary Holiday Avatar answered Jul 05 '26 19:07

Gary Holiday



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!