Last active
May 26, 2021 14:20
-
-
Save PrashantBhatasana/cc4b9ba9b488d892fe1f197f9078fbbd to your computer and use it in GitHub Desktop.
This is lambda function with python language that create ec2 instence.
This file contains hidden or 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 os | |
import boto3 | |
AMI = os.environ['AMI'] | |
INSTANCE_TYPE = os.environ['INSTANCE_TYPE'] | |
KEY_NAME = os.environ['KEY_NAME'] | |
SUBNET_ID = os.environ['SUBNET_ID'] | |
REGION = os.environ['REGION'] | |
ec2 = boto3.client('ec2', region_name=REGION) | |
def lambda_handler(event, context): | |
message = event['message'] | |
init_script = """#!/bin/bash | |
yum update -y | |
yum install -y httpd24 | |
service httpd start | |
chkconfig httpd on | |
echo """ + message + """ > /var/www/html/index.html | |
shutdown -h +5""" | |
instance = ec2.run_instances( | |
ImageId=AMI, | |
InstanceType=INSTANCE_TYPE, | |
KeyName=KEY_NAME, | |
SubnetId=SUBNET_ID, | |
MaxCount=1, | |
MinCount=1, | |
InstanceInitiatedShutdownBehavior='terminate', | |
UserData=init_script | |
) | |
instance_id = instance['Instances'][0]['InstanceId'] | |
print instance_id | |
return instance_id |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi !
When I test it, it says that the print is missing parentheses... Maybe that's a thing to add ! :)
(I guess your code was written originally for python 2.X