AWS Deployments using Serverless and Localstack

Installing Serverless

$ curl -o- -L https://slss.io/install | bash
$ source ~/.bashrc

Create New Service using Serverless Templates

$ serverless create --help
$ cd $HOME
$ serverless create --template aws-python3 --path helloService

Configure the Service to use Localstack

$ cd $HOME/helloService
$ npm install --save-dev serverless-localstack
$ aws configure --profile localstack
AWS Access Key ID : test
AWS Secret Access Key : test
Default region name [us-east-1]:
Default output format [json]: json
...
service: helloservice
...
...
plugins:
- serverless-localstack
provider:
name: aws
stage: local
runtime: python3.8
profile: localstack
custom:
localstack:
stages:
# Stages for which the plugin should be enabled
- local
host: http://localhost
edgePort: 4566
autostart: true
lambda:
mountCode: True
docker:
sudo: False

Localstack Docker Compose

$ mkdir $HOME/localstack
$ mkdir $HOME/localstack/tmp
$ mkdir $HOME/localstack/data
version: '3.7'
# HOST_MNT_ROOT=$HOME/localstack docker-compose up -d
services:
localstack:
container_name: localstack-main
image: localstack/localstack:latest
ports:
- "4566:4566"
environment:
- SERVICES=${SERVICES-serverless
,acm,apigateway,cloudformation,cloudwatch
,dynamodb,dynamodbstreams,ec2,es,events
,firehose,iam,kinesis,kms,lambda,rds
,route53,s3,s3api,secretsmanager,ses,sns
,sqs,ssm,stepfunctions,sts}
- DEBUG=${DEBUG- }
- LAMBDA_EXECUTOR=${LAMBDA_EXECUTOR-docker}
- KINESIS_ERROR_PROBABILITY=${KINESIS_ERROR_PROBABILITY-0}
- DOCKER_HOST=unix:///var/run/docker.sock
- DATA_DIR=/tmp/localstack/data
- HOST_TMP_FOLDER=${HOST_MNT_ROOT}/tmp
- TMPDIR=/tmp/localstack/tmp
- LAMBDA_REMOTE_DOCKER=false
volumes:
- "${HOST_MNT_ROOT}/data:/tmp/localstack/data"
- "/var/run/docker.sock:/var/run/docker.sock"
- "${HOST_MNT_ROOT}/tmp:/tmp/localstack/tmp"
$ cd $HOME/localstack
$ HOST_MNT_ROOT=$HOME/localstack docker-compose up -d

Deploying the Service to Localstack

$ cd $HOME/helloService
$ serverless deploy
Service Information
service: helloservice
stage: local
region: us-east-1
stack: helloservice-local
resources: 6
api keys:
None
endpoints:
None
functions:
hello: helloservice-local-hello
layers:
None

Invoking “hello” Lambda Function

$ serverless invoke -f hello
{
"body": "{\"message\": \"Go Serverless v1.0!
Your function executed successfully!\", \"input\": {}}",
"statusCode": 200
}

Listing/Identifying Stack Components

$ aws --profile localstack \
--endpoint-url http://localhost:4566 \
cloudformation \
describe-stacks \
--stack-name helloservice-local
$ aws --profile localstack \
--endpoint-url http://localhost:4566 \
cloudformation \
describe-stack-resources \
--stack-name helloservice-local
$ aws --profile localstack \
--endpoint-url http://localhost:4566 \
cloudformation \
describe-stack-resource \
--stack-name helloservice-local \
--logical-resource-id <LogicalResourceId Value>
$ aws --profile localstack \
--endpoint-url http://localhost:4566 \
cloudformation \
get-template -\
-stack-name helloservice-local
$ aws --profile localstack \
--endpoint-url http://localhost:4566 \
cloudformation describe-stack-resources \
--stack-name helloservice-local

...
{
"logGroups": [
{
"logGroupName": "/aws/lambda/helloservice-local-hello",
...
$ aws --profile localstack \
--endpoint http://localhost:4566 \
logs describe-log-streams \
--log-group-name \
/aws/lambda/helloservice-local-hello

...
{
"logStreams": [
{
"logStreamName": "2020/12/17/[LATEST]61a70b90",
...
$ aws --profile localstack \
--endpoint http://localhost:4566 \
logs get-log-events \
--log-group-name /aws/lambda/helloservice-local-hello \
--log-stream-name 2020/12/17/[LATEST]61a70b90

...
{
"events": [
"message": "{'statusCode': 200, 'body':
'{\"message\": \"Go Serverless v1.0!
Your function executed successfully!\"
...

Undeploying/Removing the Stack

$ cd $HOME/helloService
$ serverless remove

Conclusion

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store