AWS CLI & Lambda
Lots of letters in the title and few words. If you don’t know what the title means then read on. Otherwise you can skip to the next section.
Level Set
AWS — This stands for Amazon Web Services. That’s well and good but what does that mean? Their website states that:
Amazon Web Services (AWS) is a secure cloud services platform, offering compute power, database storage, content delivery and other functionality to help businesses scale and grow. Explore how millions of customers are currently leveraging AWS cloud products and solutions to build sophisticated applications with increased flexibility, scalability and reliability.
Cool. So it is a cloud platform. However, don’t let that simple description deceive you. AWS is far more powerful than just a platform that offers a place to host your virtual servers, databases, and files. There is far more available. Virtual networks, API gateways, content delivery networks, micro services hosting, and high availability offerings are just a sample of what AWS offers a customer. So, take some time to click through the above links and become familiar with what AWS offers. I am confident that you will be surprised.
Lambda — It is an AWS product that allows you to run code without worrying about server resources. From Amazon’s site:
AWS Lambda lets you run code without provisioning or managing servers. You pay only for the compute time you consume — there is no charge when your code is not running. With Lambda, you can run code for virtually any type of application or backend service — all with zero administration. Just upload your code and Lambda takes care of everything required to run and scale your code with high availability. You can set up your code to automatically trigger from other AWS services or call it directly from any web or mobile app.
CLI — This simply means “Command Line Interface.” It is a way of managing software or services (not just AWS) from the command line instead of pointing and clicking your way through an activity.
Why CLI?
Why would you care to learn and use CLI? It takes time to learn and everything you can do with it can also be accomplished through the web interface. The answer is simple — automation. I remember a very long time ago when I was first starting down the UNIX administration career path a mentor told me that you can always judge the competency of an administator by his weight. Yes, his weight! It was easy science, he stated. A competent administrator will automate everything: monitoring to remediation, initial configuration to decommission, and even daily tasks. (I once read where an admin had created scripts that interfaced with the coffee maker so that a cup would be waiting for him when he approached the device.) A good admin, he summarized could be observed doing nothing except watching his screen because everything had been automated. With all of that free time he would spend much of it eating. Thus, gain weight. Conversely, a new administrator would stay fit and thin by keeping busy manually performing tasks and monitoring environments.
The Meat
So you know understand the title as well as the motivation behind learning the CLI commands while using AWS Lambda. I am not going to repeat the information found throughout Amazon’s site but will instead just point to it where appropriate.
You must first set up your CLI environment. Instructions can be found here.
With CLI installed you are ready to begin deploying your code in Lambda.
NOTE: Some of this code has been collected from other websites and documentation but modified here for this publication.
Get Christopher A. Ryan’s stories in your inbox
Join Medium for free to get updates from this writer.
First, let’s set some variables:
function=hello_python
lambda_execution_role_name=lambda-$function-exec-role
lambda_execution_access_policy_name=lambda-$function-exec-access
lambda_invocation_role_name=lambda-$function-invocation-role
lambda_invocation_access_policy_name=lambda-$function-invocation
log_group_name=/aws/lambda/$functionNext, let’s create the python code that will run in the Lambda environment:
cat > ./hello_python.py<<EOF
def my_handler(event, context):
message = ‘Hello {} {}!’.format(event[‘first_name’], event[‘last_name’])
return { ‘message’: message }
EOFLet’s create the test JSON while we are at it:
cat > ./$function-data.json<<EOF{
“first_name”: “Bob”,
“last_name”: “Smith”
}EOF
zip the file so that we can upload it to Lambda:
zip -r $function.zip $function.pyNow we need to create the IAM role:
lambda_execution_role_arn=$(aws iam create-role \
--role-name “$lambda_execution_role_name” \
--assume-role-policy-document ‘{
“Version”: “2012–10–17”,
“Statement”: [
{
“Sid”: “”,
“Effect”: “Allow”,
“Principal”: {
“Service”: “lambda.amazonaws.com”
},
“Action”: “sts:AssumeRole”
}
]
}’ \
--output text \
--query ‘Role.Arn’
)We next need to create the policy for the execution of the Lambda function:
aws iam put-role-policy \
--role-name “$lambda_execution_role_name” \
--policy-name “$lambda_execution_access_policy_name” \
--policy-document ‘{
“Version”: “2012–10–17”,
“Statement”: [
{
“Effect”: “Allow”,
“Action”: [
“logs:*”
],
“Resource”: “arn:aws:logs:*:*:*”
},
{
“Effect”: “Allow”,
“Action”: [
“s3:GetObject”
],
“Resource”: “arn:aws:s3:::’$source_bucket’/*”
},
{
“Effect”: “Allow”,
“Action”: [
“s3:PutObject”
],
“Resource”: “arn:aws:s3:::’$target_bucket’/*”
}
]
}’We are now ready to create the function (change the adminuser to whatever user you have set up)
aws lambda update-function-code \
--region us-west-2 \
--function-name $function \
--zip-file fileb://$function.zip \
--role $lambda_execution_role_arn \
--handler $function.my_handler \
--runtime python2.7 \
--profile adminuser \
--timeout 15 \
--memory-size 512We are now ready to test it:
aws lambda invoke-async --function-name “$function” --invoke-args “$function-data.json”You can log into AWS CloudWatch and view the logs of this run.
Of course, you probably want to make changes to your code and test it all again. To do that, you need a way to update the function.
The first step is to update your code.
Then, you need to create a new zip file:
zip -r $function.zip $function.pyNext, you need to update your function with the new code:
aws lambda create-function \
--region us-west-2 \
--function-name $function \
--zip-file fileb://$function.zipFinally, you can execute it again:
aws lambda invoke-async --function-name “$function” --invoke-args “$function-data.json”





