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, 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. dotnet 6 was the chosen Lambda runtime.
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.
To summarise the chosen the role of the services in the chosen stack:
- SAM (AWS Serverless Application Model): A framework to simplify serverless application development that serves as an extension of CloudFormation.
- AWS Lambda: Implement business logic with serverless functions.
- API Gateway: Create APIs to enable back-end communication.
- DynamoDB: Store application/game data.
- Cognito: Managed service for authentication and authorisation.
- OpenAPI standard: Declarative syntax for creating clear and consistent APIs to be shared amongst developers.
Part 1 – Initial Setup:
- Install AWS CLI and Setup with AWS SSO:
- Install the AWS CLI, which allows you to interact with AWS services from the command line.
- Set up AWS Single Sign-On (SSO) and use the aws sso login command to refresh your credentials.
- Install SAM and Docker Desktop:
- Install the AWS Serverless Application Model (SAM) CLI, which provides a simplified way to build, test, and deploy serverless applications.
- Install Docker Desktop, as it is required for running functions locally.
- Create a SAM Project:
- Use the command sam init to create a new SAM project.
- Choose the AWS Quick Start Templates and select the Hello World Example.
- When prompted, I selected dotnet6 as the desired runtime.
- When prompted, select the desired package type (I used zip in this example).
- Change to the project directory using cd proj-name and launch VS Code with code ..
- Build and Test:
- Execute the command sam build to build the project.
- Test the project locally using sam local invoke HelloWorldFunction.
- Deploy:
- Deploy the project using sam deploy –guided.
- Review the changes to be deployed and confirm by typing ‘y‘.
- You can review the deployment in the AWS Management Console under CloudFormation.
Part 2 – Modifying the Example SAM Template Project:
- Add OpenAPI to the SAM Project:
- If you already have an API definition, copy the YAML file into the SAM project.
- In the example project, delete the Events block in the HelloWorldFunction section.
- Add OpenAPI Definitions:
- Add the OpenAPI definitions for the /hello route to the SAM template.
- Include the necessary information such as the operationId, responses, and integration details.
- Pay attention to the x-amazon-apigateway-integration block.
- Add API Resource:
- Define the HttpAPI resource in the SAM template.
- Specify the AccessLogSettings and DefinitionBody using the Fn::Transform and AWS::Include.
- Build and Test the API:
- Start the API locally using sam local start-api.
- Verify that the HelloWorldFunction is mounted at http://127.0.0.1:3000/hello [GET].
- Add Permissions:
- Grant the necessary permissions to the HelloWorldFunction using the AWS::Lambda::Permission resource in the SAM template.
- Rebuild and Deploy:
- Rebuild the project using sam build.
Deploy the changes using sam deploy –guided.
- Test with Curl or Postman:
- Test the API by sending requests to the deployed endpoint using tools like Curl or Postman.
Adding New Lambda Functions:
- Create Empty Constructor:
- To add additional functions to the same class file, create an empty constructor in the class.
- Add New Function Block to the Template:
- Add a new function block to the SAM template for each additional function.
- Specify the CodeUri, Handler, Runtime, MemorySize, Environment variables, and any required policies.
Adding Authentication with Cognito:
- Create Cognito User in the Template:
- Add a parameter for the email address of the created user in the SAM template.
- Include a UserPoolUser resource with the desired properties.
- Deploy with Parameter Override:
- Deploy the template with the CognitoUserEmail parameter override using sam deploy –parameter-overrides CognitoUserEmail=email@example.com.
- This will email a password to the user specified in the parameter.
- Initiate Auth Response and Obtain IdToken:
- Use the AWS CLI command aws cognito-idp admin-initiate-auth to get the session token.
- Respond to the challenge to change the password and obtain the IdToken.
- Test Authentication:
- Use the obtained IdToken to make authenticated requests to the API using tools like Curl.
Adding DynamoDB Integration to Lambda:
- Install Required Packages:
- Install the AWSSDK.SSO, AWSSDK.SSOOIDC, and AWSSDK.SecurityToken packages (required for local development).
- Batch Writing Data to DynamoDB:
- Convert JSON data to DynamoDB format using the aws dynamodb batch-write-item command.
Conclusion:
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.
Leave a Reply