Created
July 16, 2017 23:55
-
-
Save cwgem/b898cf04fa65dc1763a374170ab1d42c to your computer and use it in GitHub Desktop.
Lambda Python Environment SSM Doc
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
{ | |
"schemaVersion":"0.3", | |
"description":"Takes a Lambda AMI and sets up Python3 + virtualenv", | |
"assumeRole":"{{AutomationAssumeRole}}", | |
"parameters":{ | |
"SourceAmiId":{ | |
"type":"String", | |
"description":"(Required) The source Lambda compliant AMI ID.", | |
"default":"ami-f6035893" | |
}, | |
"PythonVersion":{ | |
"type":"String", | |
"description":"(Required) The Python 3 version to use.", | |
"default":"3.6.1" | |
}, | |
"OsUser":{ | |
"type":"String", | |
"description":"(Required) The ID of the non-root user.", | |
"default":"ec2-user" | |
}, | |
"InstanceIamRole":{ | |
"type":"String", | |
"description":"(Required) The name of the role that enables Systems Manager (SSM) to manage the instance.", | |
"default": "ManagedInstanceProfile" | |
}, | |
"AutomationAssumeRole":{ | |
"type":"String", | |
"description":"(Required) The ARN of the role that allows Automation to perform the actions on your behalf.", | |
"default":"arn:aws:iam::{{global:ACCOUNT_ID}}:role/AutomationServiceRole" | |
}, | |
"TargetAmiName":{ | |
"type":"String", | |
"description":"(Optional) The name of the new AMI that will be created. Default is a system-generated string including the source AMI id, and the creation time and date.", | |
"default":"PyLambdaDeployAMI-{{global:DATE_TIME}}" | |
}, | |
"InstanceType":{ | |
"type":"String", | |
"description":"(Optional) Type of instance to launch as the workspace host. Instance types vary by region. Default is t2.micro.", | |
"default":"t2.micro" | |
} | |
}, | |
"mainSteps":[ | |
{ | |
"name":"launchInstance", | |
"action":"aws:runInstances", | |
"maxAttempts":3, | |
"timeoutSeconds":300, | |
"onFailure":"Abort", | |
"inputs":{ | |
"ImageId":"{{SourceAmiId}}", | |
"InstanceType":"{{InstanceType}}", | |
"UserData":"IyEvYmluL2Jhc2gNCg0KZnVuY3Rpb24gZ2V0X2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93IC4uLi4NCiAgLi8iJFNDUklQVF9OQU1FIiAtLXJlZ2lvbiAiJFJFR0lPTiINCmVsc2UNCiAgZWNobyBBV1MtVXBkYXRlTGludXhBbWk6IFVuYWJsZSB0byBkb3dubG9hZCBzY3JpcHQsIHF1aXR0aW5nIC4uLi4NCmZp", | |
"MinInstanceCount":1, | |
"MaxInstanceCount":1, | |
"IamInstanceProfileName":"{{InstanceIamRole}}" | |
} | |
}, | |
{ | |
"name":"updateOSSoftware", | |
"action":"aws:runCommand", | |
"maxAttempts":3, | |
"timeoutSeconds":3600, | |
"onFailure":"Abort", | |
"inputs":{ | |
"DocumentName":"AWS-RunShellScript", | |
"InstanceIds":[ | |
"{{launchInstance.InstanceIds}}" | |
], | |
"Parameters":{ | |
"commands":[ | |
"set -e", | |
"yum update -y", | |
"yum install -y gcc zlib zlib-devel openssl openssl-devel" | |
] | |
} | |
} | |
}, | |
{ | |
"name":"setupPython", | |
"action":"aws:runCommand", | |
"maxAttempts":3, | |
"timeoutSeconds":3600, | |
"onFailure":"Abort", | |
"inputs":{ | |
"DocumentName":"AWS-RunShellScript", | |
"InstanceIds":[ | |
"{{launchInstance.InstanceIds}}" | |
], | |
"Parameters":{ | |
"commands":[ | |
"set -e", | |
"cd /tmp", | |
"wget https://www.python.org/ftp/python/{{PythonVersion}}/Python-{{PythonVersion}}.tgz", | |
"tar xzvf Python-{{PythonVersion}}.tgz", | |
"cd Python-{{PythonVersion}} && ./configure && make", | |
"make install", | |
"/usr/local/bin/pip3 install --upgrade pip", | |
"/usr/local/bin/pip3 install virtualenv" | |
] | |
} | |
} | |
}, | |
{ | |
"name":"setupUserEnvironment", | |
"action":"aws:runCommand", | |
"maxAttempts":3, | |
"timeoutSeconds":3600, | |
"onFailure":"Abort", | |
"inputs":{ | |
"DocumentName":"AWS-RunShellScript", | |
"InstanceIds":[ | |
"{{launchInstance.InstanceIds}}" | |
], | |
"Parameters":{ | |
"commands":[ | |
"set -e", | |
"su {{OsUser}} -c '/usr/local/bin/virtualenv ~/venv-python{{PythonVersion}}'", | |
"su {{OsUser}} -c 'aws configure set default.region {{global:REGION}}'" | |
] | |
} | |
} | |
}, | |
{ | |
"name":"stopInstance", | |
"action":"aws:changeInstanceState", | |
"maxAttempts":3, | |
"timeoutSeconds":1200, | |
"onFailure":"Abort", | |
"inputs":{ | |
"InstanceIds":[ | |
"{{launchInstance.InstanceIds}}" | |
], | |
"DesiredState":"stopped" | |
} | |
}, | |
{ | |
"name":"createImage", | |
"action":"aws:createImage", | |
"maxAttempts":3, | |
"onFailure":"Abort", | |
"inputs":{ | |
"InstanceId":"{{launchInstance.InstanceIds}}", | |
"ImageName":"{{TargetAmiName}}", | |
"NoReboot":true, | |
"ImageDescription":"AMI Generated by EC2 Automation on {{global:DATE_TIME}} from {{SourceAmiId}}" | |
} | |
}, | |
{ | |
"name":"terminateInstance", | |
"action":"aws:changeInstanceState", | |
"maxAttempts":3, | |
"onFailure":"Continue", | |
"inputs":{ | |
"InstanceIds":[ | |
"{{launchInstance.InstanceIds}}" | |
], | |
"DesiredState":"terminated" | |
} | |
} | |
], | |
"outputs":[ | |
"createImage.ImageId" | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is an EC2 Systems Manager Automation Document for setting up a Python 3.x environment for Lambda functions which use native code python modules. It does the following:
/usr/local
pip
virtualenv
~/venv-python{{PythonVersion}}
for the ec2-userWarnings
t2.micro
instancesTo setup the automation doc:
"default":"ami-f6035893"
(or similar if this document has been updated with a new ID) with"default":"[ami ID from step 15]"
us-east-1
so be sure to switch the console to the appropriate region if different.This will boot up an EC2 instance, setup the environment, stop the instance, and then create an AMI. Now in the "AMIs" part of the EC2 console you should see
PyLambdaDeployAMI-(some date here)
or whatever you named your AMI in the Automation parameters. You can now use this to boot an EC2 instance which contains the python virtual environment.From there when you login, you can simply cd into
~/venv-python{{PythonVersion}}
and runsource bin/activate
. Now install whatever python modules are needed usingpip
. When that's done:This will give you a ZIP file that you can then upload to S3 or through the lambda API directly. It's a good idea to do this using the AWS CLI, using a role that has permissions to interact with the Lambda/S3 API.