Deploy AWS Lambda Using Command-Line Tools


There are many tools that can be used to deploy and work with AWS Lambda. The Popular Deployment Tools for Serverless provides a good overview of them. In many cases, a command-line tool would be the go-to option for developers and DevOps to quickly deploy their AWS Lambda applications. Here are some of the best options out there.

Stackery

Stackery

Stackery is a serverless deployments platform for teams. After installing it, you can use its built-in dashboard to visually design and build serverless applications. It also provides a standard CLI. Stackery focuses on strong collaboration between teams and includes rollback protection, Github integration, and automatic build packaging.

Epsagon and Stackery have integration for easy deployment.

To install Stackery, run:

Linux:

curl -sSL https://ga.cli.stackery.io/linux/stackery > /usr/bin/stackery chmod +x /usr/bin/stackery

OS X

curl -sSL https://ga.cli.stackery.io/osx/stackery > /usr/local/bin/stackery chmod +x /usr/local/bin/stackery

To deploy your application, it must be hosted on GitHub or another code hosting website. The project should contain a directory named “src” and a file named “template.yaml.” The src directory should contain your application and requirements.txt, while template.yaml defines all the necessary resources needed to run your AWS Lambda (IAM roles/policies, S3 buckets, etc.).

Create your stack:

stackery create --stack-name ‘my-app’ --git-provider ‘github’ --blueprint-git-url ‘your-github-project-url’

To deploy your app to AWS, run:

stackery deploy --stack-name ‘my-app’ --env-name ‘dev’ --git-ref ‘master’

To learn more about using Epsagon and Stackery together, check out our documentation.

Pulumi

Pulumi

Pulumi is used for deploying Infrastructure as a Code onto various cloud providers. At the moment, it supports AWS, GCP, Microsoft Azure, Kubernetes, and OpenStack.

After installing your CLI tool, simply select the desired cloud provider and deploy your stack. In order to manage your deployment, you will be given a Pulumi permalink to review logs and used resources. Pulumi supports Python, Golang, and JavaScript/TypeScript.

Epsagon and Pulumi have an integration which you should definitely check out.

To install Pulumi on Linux/OS X, run the following command:

curl -sSL https://get.pulumi.com | sh

This will install Pulumi into $HOME/.pulumi. It can be called from $HOME/.pulumi/bin/pulumi. Set your ${PATH} to include $HOME/.pulumi/bin in order to call it without the full path.

Verify installation with:

pulumi version

Make sure you have AWS credentials stored in:

~/.aws/credentials

Initialize your project:

pulumi new python --dir my-test-project cd my-test-project

Edit Pulumi.yaml to look like something like this:

name: my-app runtime: python description: My test app template: config: aws:region: default: us-east-1

Create a new stack:

pulumi stack output

Deploy the app:

pulumi stack output

Get output information with:

pulumi stack output

Check our Pulumi-Epsagon integration documentation to learn more!

AWS Serverless Application Model (SAM)

Image result for aws sam

Using the AWS Serverless Application Model (AWS SAM), AWS Lambda can be deployed in a few easy steps. After you develop and test your serverless application in a local environment, it can be deployed using the sam package and sam deploy commands.

The sam package command packages code into a ZIP artifact and upload the packaged artifact onto Amazon S3, creating a packaged AWS SAM template file that is ready to be used. The sam deploy command uses an artifact to deploy the application. It can be combined with AWS CodeBuild, AWS CodeDeploy, and AWS CodePipeline to automate AWS Lambda deployments.

To install AWS SAM, run:

pip install --user aws-sam-cli

To deploy your application, first, you need the following directory structure:

  • Place README.md, template.yaml, and SampleEvent.json (optional) in the parent directory.
  • README.md should contain instructions to deploy code to AWS.
  • template.yaml is a CloudFormation template which contains definitions of necessary resources to run your code.
  • SampleEvent.json is optional and is used to test your code locally.

Package the application:

sam package --template-file template.yaml --output-template-file template-out.yaml --s3-bucket

Deploy the application to AWS:

sam deploy --template-file template-out.yaml --stack-name my-test-application --capabilities CAPABILITY_IAM --region

Upon successful deployment, you should see your stack with the following command:

aws cloudformation describe-stacks --stack-name my-test-application --query ‘Stacks[].Outputs’

The Serverless Framework

Serverless Framework

The Serverless Framework is a command-line tool for building and deploying serverless applications. After installing it, simply create a service and then deploy it. Epsagon provides a plugin for the Serverless Framework to automatically include Epsagon’s instrumentation in the deployed AWS Lambda functions, with no code changes required.

To install the Serverless Framework, run:

npm install -g serverless

Log in to the serverless platform:

serverless login

Create a new service:

serverless create --template aws-nodejs --path my-serverless-app

This will create a new directory called “my-serverless-app.”

To deploy the application, run:

serverless deploy -v

Then invoke the function:

serverless invoke -f my-serverless-app -l

To view function logs, run:

serverless logs -f my-serverless-app -l

More information about Epsagon and Serverless Framework can be found in our documentation.

Zappa

Zappa AWS Lambda deploy

Zappa is probably the most popular tool for deploying serverless applications written in Python. It’s based on AWS Lambda and Amazon API Gateway. Each service you create is defined as a virtual server on API Gateway. Its use is limited to Python micro web frameworks such as Flask or Bottle.

To install Zappa, run the following command:

pip --user install zappa

To initialize Zappa, run the following command in your application directory:

zappa init

Zappa will try to automatically detect which framework you are using and generate a configuration which will be stored in zappa_settings.json.

Here is an example configuration for a Flask application:

{ “dev”: { “s3_bucket”: “”, “app_function”: “application_name.app” } }

To deploy your application, run:

zappa deploy dev

This command will package your application. It will also create a new Lambda function on AWS, a new API Gateway resource, and all necessary WSGI routes—and link them with your newly created Lambda function. The output of the API Gateway endpoint will be displayed when the previous command finishes successfully.

Conclusion

There are many great tools for managing and deploying your AWS Lambda-based applications. Command-line is a convenient way to iterate fast in an environment that every developer is already comfortable with.

When choosing the best tool for you, keep in mind the ease-of-use, support for different services, and of course – pricing.