I have a VPC enabled Lambda function which attempts to launch an EC2 using a launch template. The EC2 launch step (run_instances) fails with the below generic network error.
Calling the invoke API action failed with this message: Network Error
I can launch an instance successfully directly from the launch template, so I think everything is fine with the launch template. I have configured the following in the launch template
- Amazon Machine Image ID
- Instance type
- Key Pair
- A network interface (ENI) which I had created before using a specific (VPC, Subnet, Secutity Group) combo.
- IAM role
The Lambda function includes the below code-
import json
import boto3
import time
def lambda_handler(event, context):
ec2_cl = boto3.client('ec2')
launch_temp = {"LaunchTemplateId": "<<Launch Template ID>>"}
resp_ec2_launch = ec2_cl.run_instances(MaxCount=1, MinCount=1, LaunchTemplate=launch_temp, SubnetId="<<Subnet ID>>")
Few things on the Lambda function-
- I have used the subnet in the run_instances() call because this is not the default vpc/subnet.
- The function is setup with the same (VPC, Subnet, Secutity Group) combo as used in the launch template
- The execution role is setup to be the same IAM role as used in the launch template
- The function as you see needs access only to the EC2, internet access is not needed
- I replaced the run_instances() with describe_instance_status (using the instance id created directly from the launch template) and got the same error.
The error is a network error, so I assume all is fine (atleast as of now) with the privileges granted to the IAM role. I'm sure there would be a different error, if the IAM role missed any policies.
Can someone indicate what I might be missing?