Automated deployment for AWS Solutions — Serverless Image Handler

João Golias
Outsmart Digital
Published in
5 min readOct 18, 2020

AWS Solutions are a set of technical reference implementations built by AWS Architects and Patterns, with the aim of helping costumers solve the most commom problems faced around the world. They are very easy to use and setup through AWS console, being very used world widely. However, finding a way of automating their deployment process is not a short job. This article brings an example of this for one of AWS Solutions: Serverless Image Handler.

AWS Serverless Image Handler

Current applications that wish to grow their users continously must face a hard problem: variety of technology around the world. Each user has a specific environment on which our code will run: their device, their internet, their techonological knowledge. It is so present that we face problems with performance in countless situations. One of them is handling with image quality.

A feature that requires showing several images at once may take different times, depending on user situation. A very commom strategy to avoid it is changing images qualities for some environments. We used to have to build all infrastructure and code for this by ourselves, until AWS Solutions implemented Serverless Image Handler. Basically it sets an AWS Lambda function (with logging, distribution, policies and more) that uses Sharp to process images. It also deploys a demo envoirment to show how it all works. A complete diagram with all services is shown below.

AWS Serverless Solution Handler Diagram

AWS has already written a step-by-step article about deployment of this solution though AWS console. To understand this documentation, let’s talk about CloudFormation.

CloudFormation

CloudFormation is a service that allows us to manage our infrastructure stack in AWS and third providers. We can use JSON or YML files as long as we follow its own syntax. This kind of file is really hard to be understood due to its verbosity. Therefore it would be really toilsome to try to transform the previous diagram fully into a file accepted by CloudFormation.

Well, so, let’s give AWS console another try. If you follow the steps of AWS article, you will be directed to a page of CloudFormation with some values already preset. It happens because AWS has already provided a JSON/YML template file with syntax accepted by CloudFormation, which can be found on this link. I have also uploaded it in a gist (which is too large to be embeded here). As you can see, it would be almost impossible to create it from the sketch.

There are several ways to automate a deploy if we have a JSON/YML that CloudFormation accepts. I’m describing two below.

AWS CLI

AWS hasa set of commands that allow us to communicate to their systems thought terminal. For creating a stack, we must use aws c loudformation, provinding a valid name and path (or URL) of file with a template describing all services to be created, as shown below:

Pay attention to two details:

  1. You must have aws-cli installed and configured (by using aws configure). More information about its configuration can be found here.
  2. We must use the capabilities option with the value CAPABILITY_NAMED_IAM. It is necessary because the CloudFormation Template that we are using needs to include resources that affect permissions in AWS Account. If you wish to learn more about create-stack you can acess this link.

Serverless Framework

Serverless is a method of providing infrastructure services on an as-used basis. By using it, the Cloud Service Provider (in our case, AWS) becames responsible for managing our server. As an exmaple, AWS Lambda may be the most famous service in AWS that uses this method. This is nothing more than a function (written in Node, Java, Python, or others) that can be attached to other AWS Services (AWS API Gateway, AWS S3, AWS Cloud Watch, and much more), allowing us to build complex applications.

A project that follows Serverless Architecture can use Serverless Framework to automate their deployments and other processes. The setup of this framework is really easy: you only need to have serverless cli installed and create a file serverles.yml. On this file, you can describe:

  1. All Lambda functions that must be deployed;
  2. Essential enviornment variables;
  3. Services that our application needs;

To create new services, you only need to create a resource section in your serverless.yml, and to use CloudFormation template syntax (for YML files) to describe the services. An example of it is shown below, but it creates a DynamoDB table (AWS non-relational database) instead of the Serverless Image Handler (only because it would be too large). If you wish to use this approach for deploying Serverless Image Handler, you can find the full YML syntax in AWS CloudFormation Designer or on this gist link.

Remember that you must provide your AWS Credentials before using sls deploy command to create all the services. Check this link out if you need help with it.

I would like to present one more (and last) way to deploy AWS Serverless Image Handler: Terraform.

Terraform

Terraform is a tool for building, managing and versioning infrastructure of our applications safetly and efficiently. It provides sturdy APIs for many Cloud Services Providers, such as AWS. These APIs syntaxes are much simpler than CloudFormation Template Syntax. However Terraform hasn’t launched specific syntax for AWS Solutions until now (10/2020).

So, for now, we must create, with Terraform, a Cloud Formation stack that contains the same template we used before. See an example below:

The template_body property in Terraform uses heredoc syntax. It makes us change a little the CloudFormation template, because it uses a syntax that heredoc also uses when it describes its outputs. This syntax is "$variable" that allows references to Terraform variables inside a string. Se bellow:

A “variable not found” error can be thrown when you try to apply the changes of your Terraform. To avoid it, just add another $ into string, beacuse heredoc will interpet it as a simple '$' string character. A full gist with these corrections can be found here.

Conclusion

This article tries to explain how to deploy AWS Solutions — Serverless Image Handler in three ways: AWS CLI, Serverless Framework and Terraform. However they can be used for any AWS Solutions, as long as we have the CloudFormation Template that describes all necessary services.

If you don’t use any of the two last ones, the best option may be AWS CLI, because it is an official way and it might be easier to find support for using it. Nevertheless, if your project already uses Serverless Framework or Terraform, it would be better not to set up a different way just to deploy an AWS Solution service.

Leia esse artigo em porguês aqui.

--

--