{"id":4,"date":"2024-12-26T14:38:36","date_gmt":"2024-12-26T14:38:36","guid":{"rendered":"https:\/2024\/12\/26\/hello-world\/"},"modified":"2026-04-18T12:15:39","modified_gmt":"2026-04-18T12:15:39","slug":"aws-serverless-2024","status":"publish","type":"post","link":"https:\/\/passerbliss.com\/index.php\/2024\/12\/aws-serverless-2024\/","title":{"rendered":"AWS serverless in 2024 : SAM, Lambda, API gateway, DynamoDB Cognito and OpenAPI standard"},"content":{"rendered":"<h2>Introduction:<\/h2>\n<p>In 2024, an AWS serverless architecture continues to be a popular choice for building scalable and cost-effective applications. With a service stack using SAM (AWS Serverless Application Model), AWS Lambda,\u00a0 API Gateway, DynamoDB, Cognito, and OpenAPI standard; developers can create serverless solutions with ease. This guide will walk you through the process of setting up a project with these AWS serverless components. <em>dotnet 6<\/em> was the chosen Lambda runtime.<\/p>\n<p>This example was developed as an example game back-end, however this architecture can apply to a number of real-world scenarios including web applications.<\/p>\n<p>To summarise the chosen the role of the services in the chosen stack:<\/p>\n<ol>\n<li><strong>SAM (AWS Serverless Application Model):\u00a0<\/strong>A framework to simplify serverless application development that serves as an extension of CloudFormation.<\/li>\n<li><strong>AWS Lambda:\u00a0<\/strong>Implement business logic with serverless functions.<\/li>\n<li><strong>API Gateway:\u00a0<\/strong>Create APIs to enable back-end communication.<\/li>\n<li><strong>DynamoDB:\u00a0<\/strong>Store application\/game data.<\/li>\n<li><strong>Cognito:\u00a0<\/strong>Managed service for authentication and authorisation.<\/li>\n<li><strong>OpenAPI standard:\u00a0<\/strong>Declarative syntax for creating clear and consistent APIs to be shared amongst developers.<\/li>\n<\/ol>\n<h2>Part 1 &#8211; Initial Setup:<\/h2>\n<ol>\n<li>Install AWS CLI and Setup with AWS SSO:<\/li>\n<\/ol>\n<ul>\n<li>\u00a0 \u00a0Install the AWS CLI, which allows you to interact with AWS services from the command line.<\/li>\n<li>\u00a0 \u00a0Set up AWS Single Sign-On (SSO) and use the <em>aws sso login<\/em> command to refresh your credentials.<\/li>\n<\/ul>\n<ol start=\"2\">\n<li>Install SAM and Docker Desktop:<\/li>\n<\/ol>\n<ul>\n<li>\u00a0 \u00a0Install the AWS Serverless Application Model (SAM) CLI, which provides a simplified way to build, test, and deploy serverless applications.<\/li>\n<li>\u00a0 \u00a0Install Docker Desktop, as it is required for running functions locally.<\/li>\n<\/ul>\n<ol start=\"3\">\n<li>Create a SAM Project:<\/li>\n<\/ol>\n<ul>\n<li>\u00a0 \u00a0Use the command <em>sam init<\/em> to create a new SAM project.<\/li>\n<li>\u00a0 \u00a0Choose the <em>AWS Quick Start Templates<\/em> and select the <em>Hello World Example<\/em>.<\/li>\n<li>\u00a0 \u00a0When prompted, I selected\u00a0<em>dotnet6\u00a0<\/em>as the desired runtime.<\/li>\n<li>\u00a0 \u00a0When prompted, select the desired package type (I used <em>zip <\/em>in this example).<\/li>\n<li>\u00a0 \u00a0Change to the project directory using <em>cd proj-name<\/em> and launch VS Code with <em>code .<\/em>.<\/li>\n<\/ul>\n<ol start=\"4\">\n<li>Build and Test:<\/li>\n<\/ol>\n<ul>\n<li>\u00a0 \u00a0Execute the command <em>sam build<\/em>\u00a0to build the project.<\/li>\n<li>\u00a0 \u00a0Test the project locally using <em>sam local invoke HelloWorldFunction<\/em>.<\/li>\n<\/ul>\n<ol start=\"5\">\n<li>Deploy:<\/li>\n<\/ol>\n<ul>\n<li>\u00a0 \u00a0Deploy the project using <em>sam deploy &#8211;guided<\/em>.<\/li>\n<li>\u00a0 \u00a0Review the changes to be deployed and confirm by typing &#8216;<em>y<\/em>&#8216;.<\/li>\n<li>\u00a0 \u00a0You can review the deployment in the AWS Management Console under CloudFormation.<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<h2>Part 2 &#8211; Modifying the Example SAM Template Project:<\/h2>\n<ol>\n<li>Add OpenAPI to the SAM Project:<\/li>\n<\/ol>\n<ul>\n<li>\u00a0 \u00a0If you already have an API definition, copy the YAML file into the SAM project.<\/li>\n<li>\u00a0 \u00a0In the example project, delete the <em>Events <\/em>block in the <em>HelloWorldFunction<\/em> section.<\/li>\n<\/ul>\n<ol start=\"2\">\n<li>Add OpenAPI Definitions:<\/li>\n<\/ol>\n<ul>\n<li>\u00a0 \u00a0Add the OpenAPI definitions for the <em>\/hello<\/em> route to the SAM template.<\/li>\n<li>\u00a0 \u00a0Include the necessary information such as the <em>operationId<\/em>, <em>responses<\/em>, and <em>integration details<\/em>.<\/li>\n<li>\u00a0 \u00a0Pay attention to the <em>x-amazon-apigateway-integration<\/em> block.<\/li>\n<\/ul>\n<ol start=\"3\">\n<li>Add API Resource:<\/li>\n<\/ol>\n<ul>\n<li>\u00a0 \u00a0Define the <em>HttpAPI<\/em> resource in the SAM template.<\/li>\n<li>\u00a0 \u00a0Specify the <em>AccessLogSettings<\/em> and <em>DefinitionBody<\/em> using the <em>Fn::Transform and AWS::Include<\/em>.<\/li>\n<\/ul>\n<ol start=\"4\">\n<li>Build and Test the API:<\/li>\n<\/ol>\n<ul>\n<li>\u00a0 \u00a0Start the API locally using <em>sam local start-api<\/em>.<\/li>\n<li>\u00a0 \u00a0Verify that the <em>HelloWorldFunction<\/em> is mounted at <a href=\"http:\/\/127.0.0.1:3000\/hello\">http:\/\/127.0.0.1:3000\/hello<\/a> [GET].<\/li>\n<\/ul>\n<ol start=\"5\">\n<li>Add Permissions:<\/li>\n<\/ol>\n<ul>\n<li>\u00a0 \u00a0Grant the necessary permissions to the <em>HelloWorldFunction<\/em> using the <em>AWS::Lambda::Permission<\/em> resource in the SAM template.<\/li>\n<\/ul>\n<ol start=\"6\">\n<li>Rebuild and Deploy:<\/li>\n<\/ol>\n<ul>\n<li>Rebuild the project using <em>sam build<\/em>.<\/li>\n<\/ul>\n<p>Deploy the changes using <em>sam deploy &#8211;guided<\/em>.<\/p>\n<ol start=\"7\">\n<li>Test with Curl or Postman:<\/li>\n<\/ol>\n<ul>\n<li>\u00a0 \u00a0Test the API by sending requests to the deployed endpoint using tools like Curl or Postman.<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<h3>Adding New Lambda Functions:<\/h3>\n<ol>\n<li>Create Empty Constructor:<\/li>\n<\/ol>\n<ul>\n<li>\u00a0 To add additional functions to the same class file, create an empty constructor in the class.<\/li>\n<\/ul>\n<ol start=\"2\">\n<li>Add New Function Block to the Template:<\/li>\n<\/ol>\n<ul>\n<li>\u00a0 \u00a0Add a new function block to the SAM template for each additional function.<\/li>\n<li>\u00a0 \u00a0Specify the <em>CodeUri<\/em>, <em>Handler<\/em>, <em>Runtime<\/em>, <em>MemorySize<\/em>, Environment variables, and any required policies.<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<h3>Adding Authentication with Cognito:<\/h3>\n<ol>\n<li>Create Cognito User in the Template:<\/li>\n<\/ol>\n<ul>\n<li>\u00a0 \u00a0Add a parameter for the email address of the created user in the SAM template.<\/li>\n<li>\u00a0 \u00a0Include a <em>UserPoolUser<\/em> resource with the desired properties.<\/li>\n<\/ul>\n<ol start=\"2\">\n<li>Deploy with Parameter Override:<\/li>\n<\/ol>\n<ul>\n<li>\u00a0 \u00a0Deploy the template with the CognitoUserEmail parameter override using <em>sam deploy &#8211;parameter-overrides CognitoUserEmail=email@example.com<\/em>.<\/li>\n<li>\u00a0 \u00a0This will email a password to the user specified in the parameter.<\/li>\n<\/ul>\n<ol start=\"3\">\n<li>Initiate Auth Response and Obtain IdToken:<\/li>\n<\/ol>\n<ul>\n<li>\u00a0 \u00a0Use the AWS CLI command <em>aws cognito-idp admin-initiate-auth<\/em> to get the session token.<\/li>\n<li>\u00a0 \u00a0Respond to the challenge to change the password and obtain the IdToken.<\/li>\n<\/ul>\n<ol start=\"4\">\n<li>Test Authentication:<\/li>\n<\/ol>\n<ul>\n<li>\u00a0 \u00a0Use the obtained <em>IdToken<\/em> to make authenticated requests to the API using tools like Curl.<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<h3>Adding DynamoDB Integration to Lambda:<\/h3>\n<ol>\n<li>Install Required Packages:<\/li>\n<\/ol>\n<ul>\n<li>\u00a0 \u00a0Install the <em>AWSSDK.SSO, AWSSDK.SSOOIDC<\/em>, and<em> AWSSDK.SecurityToken<\/em> packages (required for local development).<\/li>\n<\/ul>\n<ol start=\"2\">\n<li>Batch Writing Data to DynamoDB:<\/li>\n<\/ol>\n<ul>\n<li>\u00a0 \u00a0Convert JSON data to DynamoDB format using the <em>aws dynamodb batch-write-item<\/em> command.<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<h2>Conclusion:<\/h2>\n<p>By leveraging a serverless stack of SAM, Lambda, API Gateway, DynamoDB, Cognito, and the OpenAPI standard you can enjoy the benefits of serverless computing in reduced operational overhead, automatic scaling and pay-as-you-go pricing, allowing you to focus more on innovation and less on infrastructure management.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction: In 2024, an AWS serverless architecture continues to be a popular choice for building scalable and cost-effective applications. With a service stack using SAM (AWS Serverless Application Model), AWS Lambda,\u00a0 API Gateway, DynamoDB, Cognito, and OpenAPI standard; developers can create serverless solutions with ease. This guide will walk you through the process of setting [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[12,13],"class_list":["post-4","post","type-post","status-publish","format-standard","hentry","category-tech","tag-aws","tag-serverless"],"_links":{"self":[{"href":"https:\/\/passerbliss.com\/index.php\/wp-json\/wp\/v2\/posts\/4","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/passerbliss.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/passerbliss.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/passerbliss.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/passerbliss.com\/index.php\/wp-json\/wp\/v2\/comments?post=4"}],"version-history":[{"count":3,"href":"https:\/\/passerbliss.com\/index.php\/wp-json\/wp\/v2\/posts\/4\/revisions"}],"predecessor-version":[{"id":18,"href":"https:\/\/passerbliss.com\/index.php\/wp-json\/wp\/v2\/posts\/4\/revisions\/18"}],"wp:attachment":[{"href":"https:\/\/passerbliss.com\/index.php\/wp-json\/wp\/v2\/media?parent=4"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/passerbliss.com\/index.php\/wp-json\/wp\/v2\/categories?post=4"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/passerbliss.com\/index.php\/wp-json\/wp\/v2\/tags?post=4"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}