Creating and Deploying using AWS CLI



AWS CLI is a command line tool which helps to work with AWS services. We can use it to create, update, delete, invoke aws lambda function. In this chapter, you will discuss about installation and usage of AWS CLI in detail.

Installation of AWS CLI

This section will guide you through the installation of AWS CLI on various operating systems. Follow the steps given and observe corresponding screenshots wherever attached.

For Windows

Check your Windows configuration and choose one of the following links for installing AWS CLI MSI −

Once you choose corresponding link and click it, you can find a Window as shown here −

Install AWS

Next, set the Environment path in windows as shown in the screenshots below −

Environment Path

Once done, you can use the following command on the command prompt, to see if aws cli is installed −

aws --version

It displays the details of aws-cli version as shown in the following screenshot −

AWS Cli Version

For Linux / Mac

For installing on Linux and Mac, you need Python 2.6.3 or higher verison of it. Then, use following commands for further installation processes −

$ curl "https://s3.amazonaws.com/aws-cli/awscli-bundle.zip" -o "awscli-bundle.zip"
$ unzip awscli-bundle.zip
$ sudo ./awscli-bundle/install -i /usr/local/aws -b /usr/local/bin/aws

Now, we need to configure AWS settings. You can use the following command for this purpose −

aws configure

For this purpose, it requires details such as −

  • AWS Access Key ID
  • AWS Secret Access Key
  • Default region name
  • Default output from format

You can obtain these details from your aws console. Go to you Account name at top right corner as shown −

Helpful Tips

Now, click My Security Credentials and select users from left side. Add user with details as asked.

Security Credentials

Add the user and to get the access key and secret key. To see the new access key, choose Show. Your credentials will look like as shown below −

Access key ID − AOSAIOSFOCDD7Example

Secret access key − aJuirCVtnROUN/K7MDENG/bPxRfiCYExampleKEY

Access Key

Reference Commands for AWS CLIS

The following table will give command references available to work with aws cli.

Name of aws cli command Command reference
create-function create-function --function-name <value> --runtime <value> --role <value> --handler <value> [--code <value>] [--description <value>] [--timeout <value>] [--memory-size <value>] [--environment <value>] [--kms-key-arn <value>] [--tags <value>] [--zip-file <value>] [--cli-input-json <value>]
list-functions list-functions [--master-region <value>] [--function-version <value>] [--max-items <value>] [--cli-input-json <value>] [--starting-token <value>] [--page-size <value>] [--generate-cli-skeleton <value>]
get-function get-function --function-name <value> [--qualifier <value>] [--cli-input-json <value>] [--generate-cli-skeleton <value>]
get-function-configuration get-function-configuration --function-name <value> [--qualifier <value>] [--cli-input-json <value>] [--generate-cli-skeleton <value>]
get-account-settings get-account-settings [--cli-input-json <value>] [--generate-cli-skeleton <value>]
update-function-configuration update-function-configuration --function-name <value> [--role <value>] [--handler <value>] [--description <value>] [--timeout <value>] [--memory-size <value>] [--vpc-config <value>] [--environment <value>] [--runtime <value>] [--dead-letter-config <value>] [--kms-key-arn <value>] [--tracing-config <value>] [--revision-id <value>] [--cli-input-json <value>] [--generate-cli-skeleton <value>]
update-function-code update-function-code --function-name <value> [--zip-file <value>] [--s3-bucket<value>] [--s3-key <value>] [--s3-object-version <value>] [--publish | --no-publish] [--dry-run | --no-dry-run] [--revision-id <value>][--cli-input-json <value>][--generate-cli-skeleton <value>]
delete-function delete-function --function-name <value> [--qualifier <value>] [--cli-input-json <value>] [--generate-cli-skeleton <value>]

Now, let us discuss these commands one by one in detail.

create-function

This api will create a new lambda function. The code needs to be given in zip format. If the function to be created already exists, the api will fail. Note that the function name is case-sensitive.

Commands Included

The list of commands that you can use with create-function is given here −

create-function 
--function-name <value>
--runtime <value>
--role <value>
--handler <value>
[--code <value>] 
[--description <value>] 
[--timeout <value>] 
[--memory-size <value>] 
[--environment <value>] 
[--kms-key-arn <value>] 
[--tags <value>] 
[--zip-file <value>] 
[--cli-input-json <value>]

Options Included

Various options that you can use with the functions above are as follows −

--function-name (string) − This takes the name of the function. The name can be 64-bit characters.

--runtime(string) − Here you need to specify the runtime environment ie the language selection. The details of the runtime are as given below −

Options available runtime
Python v3.6 python3.6
Python v2.7 python2.7
NodeJS v6.10 nodejs6.10
NodeJS v8.10 nodejs8.10
Java java8
C# 1 dotnetcore1.0
C# 2 dotnetcore2.0
Go go1.x

--role(string) − This will be the name of the lambda policy ie the role to be given to the lambda function for accessing other services. It will have the permission as per the role specified.

--handler (string) − This is the name of the handler where the lambda code execution will start.

  • For nodejs, handler name is the module name that we export.
  • For java, it is package.classname :: handler or package.classname
  • For python, handler is nameofthefile.

--code (structure) −AWS Lambda code

--description (string) − description for the AWS Lambda function

--timeout (integer) − timeout will have the time at which the lambda function has to terminate execution. The default is 3s.

--memory-size (integer) − This is the memory given to the aws lambda function. AWS will allocate the amount of CPU and memory allocation based on the memory given.

--environment (structure) − its a object with environment details required in the aws lambda function.

e.g : Variables = {Name1 = string, Name2 = string}

--kms-key-arn (string) − this is amazon resource name (ARN) used to encrypt the environment variables. If not provided it will take the default settings to encrypt.

--zip-file (blob) − path of the zip file which has the details of the code.

--cli-input-json (string) : Performs service operation based on the JSON string provided. The JSON string follows the format provided by --generate-cli-skeleton. If other arguments are provided on the command line, the CLI values will override the JSON-provided values.

Now, let us create a simple AWS Lambda function using runtime as nodejsand add some console.logs to be printed.

Consider a sample code for understanding the same −

exports.handler = async (event) => {
   console.log("Using aws cli");
   return 'Hello from Lambda from aws cli!'
};

Now, zip the file and store it as awscli.zip.

Getting ARN

For the role, let us use the arn from the existing role we have created. To get the ARN, you will have to follow the steps as shown here. Observe the respective screenshots wherever attached −

Step 1

Go to IAM and select the role you want from Roles. The ARN details for the role are displayed as shown below. Use Role ARN with create-function in aws cli.

Summary Cli

Observe here that the role arn is : arn:aws:iam::625297745038:role/lambdaapipolicy

The command with values for create-function is as follows −

aws lambda create-function 
--function-name "awslambdausingcli" 
--runtime "nodejs8.10" 
--role "arn:aws:iam::625297745038:role/lambdaapipolicy" 
--handler "awscli.handler" 
--timeout 5 
--memory-size 256 
--zip-file "fileb://awscli.zip"

Now, if you run the command in aws cli, you can find an output as shown below −

Command Cli

In AWS console, the Lambda function is displayed as shown below −

Cli Function

The details of the functions are as shown here −

Cli Code

The details of the configuration are as given below −

Cli Role

You can test the function and check the output as shown −

Cli Execution

The corresponding Log output is shown here −

Cli Output

list-functions

This api gives the list of functions created so far in AWS Lambda.

Commands Included

The following are the commands asscociated with this API −

list-functions
[--master-region <value>]
[--function-version <value>]
[--max-items <value>]
[--cli-input-json <value>]

Options under list-functions

The following are various options you can use under this list-functions api −

--master-region(string) − optional. The region from which the functions needs to be displayed.

--function-version(string) − optional. This will give the function version.

--max-items(integer) − optional. This will give the items as the per the value specified.

--cli-input-json(string) − optional. Will perform operation based on the json file provided.

The command with values list-functions is as follows −

aws lambda list-functions --max-items 3

The command displays details as follows −

Cli Display

get-function

This api will give details of the functions and also a url link which has zip file uploaded using create-function. The url with zip details will be valid only for 10 mins.

Commands Included

The following are the commands associated with this api −

get-function
--function-name <value>
[--qualifier <value>]
[--cli-input-json <value>]
[--generate-cli-skeleton <value>]

Options Included

--function-name − Name of the AWS Lambda function. You can also specify Amazon Resource Name of the function.

--qualifier(string) − Optional. Function version can be used to get the details of the function.

The command with values to get-function are −

aws lambda get-function --function-name awslambdausingcli

The command display details are as follows −

Option Included

It gives the url which has the zip code uploaded. In the above case the url is −

https://prod-04-2014-
tasks.s3.amazonaws.com/snapshots/625297745038/awslambdausingcli-97048f8d-4a08
-4ed9-99d9-acb00d2063d2?versionId=d04HKvPu9S2zz8pzjbW6Rmf5o5fxnc_r&X-Amz-Security
-Token=FQoDYXdzEKT%2F%2F%2F%2F%2F%2F%2F%2F%2F%2FwEaDCpTmGvtwKToPBiWcyK3A96UcJEnwvYDhMbbxu
%2Bg2gffK2ocfnlEeiCHak8QqqE1RFpbKrdks9NzxP9gNbagL4M9RValxJ1a9PUY%2FOdAekscRHOiX00MVAxUlI8
2pKryhdOwLJWSj0uRzqvOxCcBwJenHrSNPeG6lMa2ZDo0qZFEUDONSaTg4nuSnJK1f6t3pMAKu4vF9wPvf92G%2BU
60rUxwleggigISmD9l1IlZse3%2BVF1JlNuN%2F5d85v0y2Q%2F%2BO515CybcZpn91sHPYG8JMJ00LsrkQ2Ww4VU
9Zz5c5QYH4JYPj0CyEgSz9b%2FMceMPpOoPUAMjctb%2FEwQqcShZeqAr9%2Fcd2ZI%2BXl2%2Bs4ri0ucgPvQQvs
eGIIiZbX3GqdwR2jb1nylrAEIfiuFMoSWfcFYoYtuL0MZnjGR9jy2GNkp6MB%2BlHHr7%2BnuFRUzU26rgDYmdE1w
Rb3%2B21Jm49WGDa9opRLvUxFaux57Or70haib2FuKzN6Gf3Vzzk5KPdWsYUpaLyf%2B1ovEytOZhB1JEXuCs%2FG
IlOXS88yxT%2BpOKmyxweiezpGgI%2FAkSAQTbSRsYQKIOFyIJNHzplwrJKhy28vy60numIBIo9Zqq2AU%3D
&X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Date=20180527T112426Z&X-Amz-
SignedHeaders=host&X-Amz-Expires=600&X-Amz-
Credential=ASIAICSQHLSBWFP37U4Q%2F20180527%2Fus-
east-1%2Fs3%2Faws4_request&X-Amz-Signature=
8b97e7d6d7da13313068e027894d2c875be5e50a0c5a62550f55307985bdc1aa

get-function-configuration

This will give the configuration details of the AWS Lambda function.

The following are the commands used along with this api −

get-function-configuration
--function-name <value>
[--qualifier <value>]

The following are the options used with

--function-name (string) − name of the aws lambda function. You can also specify Amazon Resource Name of the function.

--qualifier(string) − Optional.Function version can be used to get the details of the function.

The command with values to get-function are −

aws lambda get-function-configuration --function-name awslambdausingcli

The command displays details as follows −

Get Function.

get-account-settings

This api gives the accounts settings.

Commands Involved

The command that you can use with this api are −

get-account-settings
[--cli-input-json <value>]
[--generate-cli-skeleton <value>]

Options Involved

You can use the following options with this api −

--cli-input-json(string) − Performs the service based on the json string provided.

--generate-cli-skeleton(string) − It prints json output without sending the API request.

You can use the following command for get-account-settings −

aws lambda get-account-settings

You can see the following output when you execute the command given above −

Option Involved

update-function-configuration

This api helps to update the configuration details for AWS Lambda function created. You can change the memory, timeout, handler, role, runtime, description etc.

Commands Involved

The following are the commands involved in the update-function-configuration api −

update-function-configuration
--function-name <value>
[--role <value>]
[--handler <value>]
[--description <value>]
[--timeout <value>]
[--memory-size <value>]
[--environment <value>]
[--runtime <value>]
[--cli-input-json <value>]
[--generate-cli-skeleton <value>]

Options Involved

The following are the options involved in update-function-configuration api −

--function-name − name of the aws lambda function

--role (string) − optional. The ARN of role is needed to be updated.

--handler (string) − optional. The handler details of aws lambda function.

--description(string) − optional. Description for the function.

--timeout(integer) − optional. Time required so that aws lambda function can terminate.

--memory-size(integer) − optional. This is the memory given to the aws lambda function. AWS will allocate the amount of CPU and memory allocation based on the memory given.

--environment (structure) − optional. It is an object with environment details required in the aws lambda function.

e.g: Variables = {Name1 = string, Name2 = string}

--runtime(string) − Here you need to specify the runtime environment ie the language selection.

The details of the runtime are shown in the table given below −

Options available runtime
Python v3.6 python3.6
Python v2.7 python2.7
NodeJS v6.10 nodejs6.10
NodeJS v8.10 nodejs8.10
Java java8
C# 1 dotnetcore1.0
C# 2 dotnetcore2.0
Go go1.x

--cli-input-json (string) − optional. This will perform the operation on the api as specified in the json string provided.

--generate-cli-skeleton (string) − optional. This will output the JSON skeleton of all details without executing the api. The output can be used as a input to --cli-input-json.

Now, let us chage the memory and timeout of AWS Lambda function that we have created earlier. Follow the Steps given below and observe the corresponding screenshots attached for this purpose −

Step 1

The memory and timeout before the change occurred is as follows −

Timeout

Step 2

Now, with update-function-configuration, let us change the memory and timeout to 320MB and timeout to 10s. For this purpose, use the following command with values −

aws lambda update-function-configuration --function-name “awslambdusingcli” 
--timeout 10 --memory-size 320

Step 3

Then you can see the following output as the display −

Cli Updated

Step 4

The display in AWS console after using update-function-configuration is as follows −

Cli Configuration

Update-function-code

This api will update the code for an existing AWS Lambda function.

Commands Involved

update-function-code
--function-name <value>
[--zip-file <value>]
[--s3-bucket <value>]
[--s3-key <value>]
[--s3-object-version <value>]
[--cli-input-json <value>]
[--generate-cli-skeleton <value>]

Options Involved

The following are the options involved with the update-function-code api −

--function-name(string) − name of aws lambda function

--zip-file (blob) − optional. Path of the zip file which has the code to be updated.

--s3-bucket(string) − optional. S3 bucket name which has the zip file with code uploaded.

--s3-key(string) − optional. AWS s3 object key name which has to be uploaded.

--s3-object-version (string) − optional. AWS s3 object version.

--cli-input-json (string) − optional. This will perform the operation on the api as specified in the json string provided.

--generate-cli-skeleton (string) − optional. This will output the JSON skeleton of all details without executing the api. The output can be used as a input to --cli-input-json.

The updated code is as shown below −

exports.handler = async (event, context) => {
   console.log("Using aws cli");
   console.log()
   return 'Hello from Lambda from aws cli!'
};

You can use the following command with values for this purpose

aws lambda update-function-code --function-name "awslambdausingcli" 
--zip-file "fileb://awscli.zip"

The corresponding output is as shown here −

Cli Purpose

The display from AWS console is as shown here −

AWS Cli Console

The corresponding log output is as shown below −

Cli Log

delete-function

The delete aws cli api will delete the function given.

Commands Included

The command details for the same are given here −

delete-function
--function-name <value>
[--qualifier <value>]
[--cli-input-json <value>]
[--generate-cli-skeleton <value>]

Options Included

The options included in this api are as given below −

--function-name(string) − this will take the lambda function name or the arn of the aws lambda function.

--qualifier (string) − This is optional. Here you can specify the version of aws lambda that needs to be deleted.

-- cli-input-json(string) − Performs service operation based on the JSON string provided. The JSON string follows the format provided by --generate-cli-skeleton. If other arguments are provided on the command line, the CLI values will override the JSON-provided values.

--generate-cli-skeleton(string) − it prints json skeleton to standard output without sending the API request.

You can use the following command with values for this purpose −

aws lambda delete-function --function-name "lambdatestcli"

Delete Function

Now, observe that the function will not be seen in AWS Lambda function list −

Cli Keywords
Advertisements