Created
November 19, 2019 21:56
-
-
Save beothorn/845e43c0e231f4413b29829337fc05fd to your computer and use it in GitHub Desktop.
lambda request ec2
This file contains 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 boto3 | |
import datetime | |
import os | |
import json | |
def test_instance(ec2_client): | |
ec2_client.request_spot_instances( | |
DryRun=False, | |
SpotPrice='0.02', | |
ClientToken='string', | |
InstanceCount=1, | |
Type='one-time', | |
LaunchSpecification = { | |
'ImageId': 'ami-ami-myAmi', | |
'InstanceType':'t3a.nano', | |
'Placement':{}, | |
'SecurityGroupIds':[ | |
'sg-mySecurityGroups' | |
] | |
} | |
) | |
def detailed_instance(ec2_client): | |
ec2_client.request_spot_instances( | |
DryRun=False, | |
SpotPrice='0.02', | |
ClientToken='string', | |
InstanceCount=1, | |
Type='one-time', | |
LaunchSpecification={ | |
'ImageId': 'ami-myAmi', | |
'SecurityGroups': ['sg-mySecurityGroups'], | |
'InstanceType': 't3.medium', | |
'BlockDeviceMappings': [ | |
{ | |
'Ebs': { | |
'SnapshotId': 'snap-mySnap', | |
'VolumeSize': 100, | |
'DeleteOnTermination': True, | |
'VolumeType': 'gp2', | |
'Iops': 300, | |
'Encrypted': False | |
}, | |
}, | |
], | |
'EbsOptimized': True, | |
'Monitoring': { | |
'Enabled': True | |
}, | |
'SecurityGroupIds': [ | |
'mySecurityGroupIds', | |
] | |
} | |
) | |
def lambda_handler(event, context): | |
ec2_client = boto3.client('ec2', | |
aws_access_key_id = os.environ.get('AWS_ACCESS_KEY_ID'), | |
aws_secret_access_key = os.environ.get('AWS_SECRET_ACCESS_KEY'), | |
region_name = 'us-west-2' | |
) | |
print(test_instance(ec2_client)) | |
return { | |
'statusCode': 200, | |
'body': json.dumps('ec2 requested!') | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment