Getting Started with AWS SAM

In the previous post, we talked about AWS SAM and what is a serverless application. If you have not yet read about it click on the links and read the articles first.

In this post, we will look into setting up the AWS SAM CLI on our environment and look at some of the commands to get started with it.

Pre-requisites

You will need an AWS account. If you don\’t have one, sign-up for it in the free tier for setting it up. Create an IAM user and generate the credentials associated with it. We will need these credentials when we will set up our environment.

Once you have set up the above, install the aws-cli on your machine and use the following commands to set up credentials in your terminal.

export AWS_ACCESS_KEY_ID=your_access_key_idexport AWS_SECRET_ACCESS_KEY=your_secret_access_keyexport AWS_REGION=the_region_you_are_using

Now we need to install the sam-cli to start using AWS SAM for our application deployment. The following links will give you official AWS steps as per your operating system. Please follow the links and install the sam-cli on your machine.

Let\’s test whether it is installed properly or not. type the following command on your terminal.

sam --version

If everything is installed properly and set up, you will see the installed version of AWS SAM.

Some of the command which will help you with AWS SAM are:

sam init

This command will help you initialize a project with SAM default configuration for a language. You can also download and use a sample application provided by AWS.

The output from the above command will look something like the following:

Which template source would you like to use?	1 - AWS Quick Start Templates	2 - Custom Template LocationChoice: 1What package type would you like to use?	1 - Zip (artifact is a zip uploaded to S3)	2 - Image (artifact is an image uploaded to an ECR image repository)Package type: 1Which runtime would you like to use?	1 - nodejs14.x	2 - python3.8	3 - ruby2.7	4 - go1.x	5 - java11	6 - dotnetcore3.1	7 - nodejs12.x	8 - nodejs10.x	9 - python3.7	10 - python3.6	11 - python2.7	12 - ruby2.5	13 - java8.al2	14 - java8	15 - dotnetcore2.1

It will be an interactive and guided flow to start with. I will write another post where we will build an AWS Lambda function using javascript and deployed it on a Node.js environment using AWS SAM.

The other command is to build and run it locally if you want or deploy it directly to AWS.

sam build

This command will build the application and prepare a SAM package to be deployed on the AWS. The command uses the template file defined in the project to create to prepare the build.

Finally, to deploy the app on AWS you can use another guided tutorial using the following command

sam deploy --guided

The above command will package and push the artefacts to an S3 bucket so it can be used by the lambda function while processing the code. This command will also read your template file and figure out which all resources need deployment or updation and take the necessary steps to make modifications as per your desired state of infrastructure.

These three commands will get you started with the AWS SAM. In the future post, we will talk about and see some other commands which we can use to deploy the applications on AWS. We will also see how can we automate the deployment process by using AWS SAM in CI/CD pipelines.

Happy Coding :).