Latest news about Bitcoin and all cryptocurrencies. Your daily crypto news habit.
Introduction
A few years ago AWS launched static hosting service S3, which was a paradigm shift for hosting static websites. The tech was crystal clear, all the static assets (HTML, CSS, and JS) would reside in an S3 bucket to host your impressive website. A pretty cool idea I personally liked it, really. Had it not been for that super important contact form hosting on S3 would have been cool but your contact form would be a joke unless you had another server in place to service AJAX requests from that form. The moment you had that service ready, the S3 solution wouldnât appear so attractive at all.
In the age of cutting edge technology, thereâs always jaw-dropping innovations around the corner. One of the awesome tech innovation happens to be serverless. Not that there are no servers involved but you can care less about them now. Serverless can be a proper and viable solution to a lot of problems, it is the most perfect solution for your static hosted contact form. Keep reading by the end of this post you will be able to handle your website forms in the most inexpensive and simplest manner possible.
The Serverless Framework
Source: https://serverless.com/Serverless is your toolkit for deploying and operating serverless architectures. Focus on your application, not your infrastructure.â Serverless.com
The Swiss army knife of Serverless technologies. Serverless Framework is a free and open-source web framework written in Node.js. Serverless was the first framework to be developed for building applications exclusively on AWS Lambda, the serverless computing platform provided by Amazon Web Services. Currently, applications developed with Serverless Framework can be deployed to other FaaS service providers. Here is the list of the Serverless cloud services supported by the Serverless Framework:
- AWS Lambda
- Google Cloud Functions
- Azure Functions
- IBM OpenWhisk
- Auth0 Webtask
- Oracle Fn Project
- Spotinst
- Kubeless
Getting started with Serverless Framework
Obviously, you are pretty excited to get started with the Serverless Framework, letâs cut to the chase and start by installing Serverless.
Setting up Serverless is simple. You need to install it through npm and link it to your AWSÂ account.
1. Installing Serverless Globally
Time to get hands-on Serverless stuff.
$ npm install serverless -g
This command installs Serverless globally on your local machine. The Serverless commands are now available to you from your terminal.
Note: Running Linux, you may want to run the above command as sudo.
2. Create an IAM user in the AWSÂ Console
Go to your AWS Console, you will find the IAM service listed below the âSecurity, Identity & Complianceâ group. Inside the IAM dashboard click on the Users tab and click âAdd Userâ button.
Create a new user and allow the user programmatic access by clicking on the Programmatic access checkbox. Next, in the permissions section, you need to add a set of permissions to the user. From the list of available options under the âAttach existing policies directlyâ check the AdministratorAccess.
After the user is created, you will have access to the users Access Key ID and Secret Access Key. You will be required to use these keys in the next step.
Word of Caution: These are the kind of credentials you donât want to lose even by mistake, remember you have provided AdministratorAccess to this user. The user with AdministratorAccess can do pretty much everything with your AWSÂ account.
3. Configuring Serverless to use IAM Credentials
Great! With the keys, you can set up Serverless Framework to access your AWS account. Switch to your terminal and use this command to configure Serverless:
$ sls config credentials --provider aws --key xxxxxxxxxxxxxx --secret xxxxxxxxxxxxxx --profile <username>
Now your Serverless installation knows what account AWS to connect.
Note: sls is an alias for the serverless command. You can use both to the same effect. But sls is kinda cool.
4. Creating a service
With the Serverless Framework hooked up with your AWS account, you can set up a Serverless project in a jiffy. Fire up the terminal and issue the following command:
$ sls create --template aws-python --path <your-folder-path>
The --template flag is used to specify a preset template with the given settings. In the above command the template aws-python will set up the project configured to use AWS as the provider and Python as the runtime. The command will auto-generate serverless.yml , handler.py and .gitignore file with preset values.
The configuration is defined in the serverless.yml file. This file is the most important file in the Serverless Framework. Itâs almost magical, given how it can spin up the infrastructure you have defined in it. The contents of the auto-generated serverless.yml file will look something like this:
service: <your-service-name>
provider: name: aws runtime: python2.7
functions: hello: handler: handler.hello
The provider section defines everything related to the service provider, there are a lot more properties to configure it further you can take a look at them here. In the auto-generated serverless.yml file, you need to add two important tags under the provider section, which are as follows:
region: <your-aws-region>profile: <aws-username-with-programmatic-access>
The functions property is used to declare the serverless functions, you can declare multiple functions under this property. The above example declares a function called hello present in the handler.py file. Browse over to the handler.py file and you will find something like this:
import json
def hello(event, context): body = { "message": "Go Serverless v1.0! Your function executed successfully!", "input": event } response = { "statusCode": 200, "body": json.dumps(body) } return response
The Serverless App
Our Serverless solution makes use of AWS infrastructure, it consists of API Gateway, Lambda Functions, DynamoDB and Simple Email Service(SES). To achieve this end result we will use the previously introduced Serverless Framework.
Architecture of the Serverless app
- Static WebsiteâââAmazon S3 provides a robust and simple web server. All of the static HTML, CSS and JS files for your application can be served from S3. The contact form on our static website is submitted using AJAX.
- API GatewayâââThe API Gateway is the event source for the application, it acts as a bridge between our contact form and serverless lambda function. It routes the request from the contact form to the lambda function. The API Gateway also performs tasks such as access control, monitoring, API version control and traffic management.
- AWS LambdaâââAWS Lambda is the place where the action takes place. Lambda functions run in stateless compute containers that are event-triggered, managed and ephemeral. In our example, we use a lambda function to send email using SES and store the request contents in DynamoDB NoSQL database.
- Simple Email Service (SES)âââThe cloud-based email sending service from Amazon. Scalable email service, you can send marketing and transactional emails using SES. In our example, we use SES to send emails using a verified email address.
- DynamoDBâââDynamoDB provides a scalable, consistent, fully managed and non-relational database from Amazon. In our example, we use DynamoDB to store and retrieve the messages received from the static contact form.
You can find the source code for the demo application here. Go ahead and clone it!
faizanbashir/python-ses-dynamodb-contactform
Application Walkthrough
Letâs have a stroll through the demo application before we actually deploy it on AWS.
1. Demystifying the serverless.yml file
The serverless.yml file defines the services the application needs to use and interact with. The resources and the actions the Serverless functions can perform are listed under the iamRoleStatements property. It lists the actions and resources.
iamRoleStatements: - Effect: "Allow" Action: - ses:SendEmail - ses:SendRawEmail Re
Disclaimer
The views and opinions expressed in this article are solely those of the authors and do not reflect the views of Bitcoin Insider. Every investment and trading move involves risk - this is especially true for cryptocurrencies given their volatility. We strongly advise our readers to conduct their own research when making a decision.