Deploy a Serverless API to Amazon Web Services (AWS)

//this._tableName = 'contacts'; this._tableName = tableName;}…# seed/runner.js# Extract the 'CONTACTS_TABLE' environment from process.env and# use it in the instantiation of the 'ContactSeeder'.# Optionally, update the 'log' statements with 'CONTACTS_TABLE'# instead of using the hard coded 'contacts' string.const { CONTACTS_TABLE } = process.env;……const contactSeeder = new ContactSeeder(dynamo, doclient, CONTACTS_TABLE);# running 'npm run seed' should now work properly.PackagingWe should do one final verification by packaging the API before we actually deploy to AWS.Serverless includes a package command that will create the deployment package locally in a temp .serverless folder for us to inspect.Run the npm run package script we added earlier to the package.json file, then open up the .serverless folder.$ npm run package> contacts-api@1.0.0 package ./contacts_api> serverless packageServerless: Packaging service…Serverless: Excluding development dependencies…Open the .serverless folder and extract the contacts-api.zip file..Expand the folders and make sure that only node_modules and src are included in the zip.The contacts-api folder should look like this:.serverless folderDeploy to AWSNow that we have confirmed that all of our changes work locally, let’s deploy to AWS and see if it works there.Notice that the npm run deploy script calls, serverless deploy –aws-profile serverless..This matches the AWS credentials we set up at the beginning of this article.$ npm run deploy> contacts-api@1.0.0 deploy /contacts_api> serverless deploy –aws-profile serverlessServerless: Packaging service…Serverless: Excluding development dependencies…Serverless: Uploading CloudFormation file to S3…Serverless: Uploading artifacts…Serverless: Uploading service .zip file to S3 (15.45 KB)…Serverless: Validating template…Serverless: Updating Stack…Serverless: Checking Stack update progress……………………………………………………….Serverless: Stack update finished…Service Informationservice: contacts-apistage: devregion: us-west-2stack: contacts-api-devapi keys: Noneendpoints: GET – https://<auto-gen>.amazonaws.com/dev/contacts GET – https://<auto-gen>.amazonaws.com/dev/contact/{id} POST – https://<auto-gen>.amazonaws.com/dev/contact PUT – https://<auto-gen>.amazonaws.com/dev/contact/{id} DELETE – https://<auto-gen>.amazonaws.com/dev/contact/{id}functions: list: contacts-api-dev-list get: contacts-api-dev-get add: contacts-api-dev-add update: contacts-api-dev-update delete: contacts-api-dev-deletelayers: NoneWe can now call each of the endpoints to test if they work, starting with list.$ curl -i https://<auto-gen>.amazonaws.com/dev/contactsHTTP/2 200 content-type: application/jsoncontent-length: 2……# An empty array because we haven't posted or seeded data.[]Posting data (add).$ curl -i -H 'Content-type: application/json' -X POST -d '{"id": "1", "firstName": "Jin", "lastName": "Erso"}' https://<auto-gen>.amazonaws.com/dev/contactHTTP/2 201 content-type: application/jsoncontent-length: 0……Fetching contacts list again to verify (list).$ curl -i https://<auto-gen>.amazonaws.com/dev/contactsHTTP/2 200 content-type: application/jsoncontent-length: 48……# There's the data we posted to the POST endpoint![{"id":"1","firstName":"Jin","lastName":"Erso"}]Fetch by id (get).$ curl -i https://<auto-gen>.us-west-2.amazonaws.com/dev/contact/1HTTP/2 200 content-type: application/jsoncontent-length: 46……# Notice that it's the contact and not an array of contacts like # when we called the GET /contacts endpoint.{"id":"1","firstName":"Jin","lastName":"Erso"}Updating a contact (update).$ curl -i -H 'Content-type: application/json' -X PUT -d '{"id": "1", "firstName": "Jin-updated", "lastName": "Erso-updated"}' https://<auto-gen>.amazonaws.com/dev/contact/1HTTP/2 200 content-type: application/jsoncontent-length: 62……# The updated contact is returned.{"id":"1","firstName":"Jin-updated","lastName":"Erso-updated"}Finally, delete.$ curl -i -X DELETE https://<auto-gen>.amazonaws.com/dev/contact/1HTTP/2 204 ……# Nothing is returned.The source branch can be found here:vanister/contacts_api/serverless-deploy-awsServerless RESTful API with AWS Lambda, API Gateway and DynamoDB – vanister/contacts_apigithub.comThe original article can be found here:Build a RESTful API using AWS Lambda, API Gateway, DynamoDB and the Serverless FrameworkUpdate: 17 October 2018.itnext.ioI mentioned running AWS/DynamoDB out of a Docker container which can be found here:Containerizing Serverless APIsUse Docker, LocalStack and the Serverless Framework for Local Developmentitnext.ioHere is the detailed guide to Serverless around AWS configurations and deployment:Serverless Framework – AWS Lambda Guide – IntroductionEdit on github The Serverless Framework helps you develop and deploy your AWS Lambda functions, along with the AWS…serverless.com. More details

Leave a Reply