Serverless Deployment: Simplifying Application Management
Updated: Nov 5
There are many ways to deploy infrastructure as code, but today’s Teratip is about a special one we like to use: Serverless.
As with many IAC tools, we start by writing a text file and then running a binary that controls the creation of what we declared. However serverless has a control advantage over the infrastructure that requires AWS resources like Lambda functions or DynamoDB. In less than 50 lines of YAML code, you can create a state-of-the-art infrastructure using S3 Buckets, DynamoDB, and more with all the required policies to keep it safe.
For example, a Yaml like the following will create an S3 bucket, a DynamoDB table, and the infrastructure for the function to communicate them:
service: quicksite
frameworkVersion: ">=1.1.0"
provider:
name: aws
runtime: nodejs10.x
environment:
DYNAMODB_TABLE: ${self:service}-${opt:stage,
self:provider.stage}-uniqname
iamRoleStatements:
- Effect: Allow
Action:
- dynamodb:Query
- dynamodb:Scan
- dynamodb:GetItem
- dynamodb:PutItem
- dynamodb:UpdateItem
- dynamodb:DeleteItem
Resource: "arn:aws:dynamodb:${opt:region, self:provider.region}:*:table/${self:provider.environment.DYNAMODB_TABLE
}"
functions:
create:
handler: fn/create.create
events:
- http:
path: fn
method: post
resources:
Resources:
MyBucket:
Type: AWS::S3::Bucket
Properties:
BucketName: ${self:service}-${opt:stage,
self:provider.stage}-uniqname
AccessControl: PublicRead
MyDb:
Type: 'AWS::DynamoDB::Table'
DeletionPolicy: Retain
Properties:
AttributeDefinitions:
- AttributeName: id
AttributeType: S
KeySchema:
- AttributeName: id
KeyType: HASH
ProvisionedThroughput:
ReadCapacityUnits: 1
WriteCapacityUnits: 1
TableName: ${self:provider.environment.DYNAMODB_TABLE}
Once you have your Yaml file, Serverless will compile it for Cloudformation making the full deployment of its content and keeping records for its modification in the future. Nice, isn't it?
Give it a try. Start at https://www.serverless.com/. Let us know if you like Serverless and we’ll keep you updated with more Teratips about it.
Juan Eduardo Castaño
DevOps Engineer
Teracloud