top of page

Serverless framework - Deploying a simple infrastructure in AWS



Here we will create a basic serverless architecture in AWS involving API Gateway, Lambda, and DynamoDB. An API will be exposed publicly, execution on which will invoke a lambda which in turn will query a table of DynamoDB for the data.


All the configuration and deployment will be done using the serverless framework.


1. Install serverless framework

npm install -g serverless

We can check the version of the installed serverless framework using commands 'serverless' or 'sls'.

serverless -v
sls -v

2. Configure AWS credentials in serverless framework

serverless config credentials --provider aws --key XXXXXXXXXX --secret YYYYYYYY --profile slsUser

3. Create a new serverless project "serverlessInfrastructure" using template.

Many different templates depending on the programming languages are available. Check all the available templates here.

In this case, we are choosing node.js

serverless create --template aws-nodejs --path serverlessInfrastructure


4. On the creation of a new serverless project in step 3, some files will be autogenerated in the project of which the main ones are serverless.yml and handler.js.

4.1. serverless.yml consists of all the configurations that the framework uses to create a serverless infrastructure in AWS.

4.2. handler.js has the code for the lambda to be created.



5. Let's just update the code to configure our required infrastructure. Rest API in API Gateway,

Lambda

DynamoDB

Find the complete code in this GitHub Repo here.


6. Initially the code repo will not have the required libraries. All the required libraries that are to be installed at the compilation time are mentioned in the file package.json.

Initially, to generate package.json use this command.

npm install --save aws-sdk

Later, after every new change we should execute the npm command to update the package.json file so that we don't have a problem in the compilation. This command generates a package-lock.json file that has the confirmation of all the installed required libraries for compilation purposes.

npm install

6. Once the code is done and the installation of libraries is successful, now we need to deploy our serverless infrastructure using the serverless framework.

sls deploy

7. On deployment, we can see that serverless has generated a cloud formation stack for the required infrastructure, which in turn configures all the required services.



Additionally, we can observe that the serverless framework has created an s3 bucket with the name 'serverlessinfrastructure--serverlessdeploymentbuck-ghyws5a7thsu' that has serverless state files and other JSON files for different services. These files are generated by the serverless framework. and are compiled versions of the code.



8. Apart from the cloud formation stack and serverless s3 bucket, we can see that our required infrastructure has been configured successfully.





9. Let's just populate the DynamoDB table with some sample data.

{"userId": 10, "age": 36, "userName": "Rahul"}
{"userId": 20, "age": 34, "userName": "Karina"}
{"userId": 30, "age": 3, "userName": "Anaya"}

10. Test API using curl command with API endpoint.


11. In case this AWS infrastructure is no more needed, just eliminate it using the remove command of the serverless framework. It will eliminate the cloud formation stack, which in turn will eliminate all the configured services in AWS.

sls remove

Thanks a lot and hope you have a basic idea of how to configure serverless infrastructure in AWS using the serverless framework. 🙂

39 views0 comments

Recent Posts

See All

Comments


bottom of page