Skip to content

Deploy auth backend service to AWS lambda (serveless), with CDK for infrastructure as code (IaC).

License

Notifications You must be signed in to change notification settings

eugengi/golambda

Repository files navigation

GoLambda

Go version Open in Dev Containers

Introduction

Serverless lambda function for a Golang, user authentication, REST API service deployed to AWS Lambda based on IaC (Infrastructure as Code) using CDK and CloudFormation.

Installing / Getting started

This is an overview of the minimal setup needed to get started.

Prerequisites

ℹ I'd recommend Microsoft's documentation to set up Docker on WSL2 if you're on Windows.

If you already have VS Code and Docker installed, you can click the badge above or here to get started. Clicking these links will cause VS Code to automatically install the Dev Containers extension if needed, clone the source code into a container volume, and spin up a dev container for use.

AWS Setup

Brief guide to set up and authenticate AWS SDK tools required for this project.

Overview

The AWS-specific, Dev Container features install the AWS toolkit extension for Vs Code. This provides a secure and easy way to authenticate the CLI and CDK tools to connect to AWS using Single-Sign-On (SSO) from inside your Dev Container. This section references guides to help you set up an AWS user account, IAM Identity Center administrative user (recommended), IAM Identity Center developer user, and the required configuration to authenticate the local SDK dev tools.

Prerequisites

An AWS account if you don't already have one.

  1. Create an AWS account
  2. Configure an AWS IAM Identity Center administrative user

Configuration

Before we can use our SDKs/tools, we need to configure programmatic access to AWS service APIs from our local environment. Since we are working in an isolated environment (devcontainer) we can leverage shared config and credential files associated with an unnamed profile, [default], which applies to all AWS project tools.

  1. Configure IAM Identity Center permissions for SDKs/tools

ℹ Ideally, you want to follow up by creating a new, non-administrative user (ie. user-local-devcontainer-golambda) in IAM Identity Center and provision this user access and permissions scoped to the AWS resources required for this project.

  1. Configure temporary credentials that automatically refresh
    Before we can connect to AWS with the AWS Toolkit, we need to provide minimal configuration to ensure that the AWS CLI and any other SDK/tool is authenticated and assigned automatically refreshing temporary credentials during the SSO process. This can be done interactively using the AWS CLI or manually using a config file (see sample file ./config.sample).

ℹ If the required files do not already exist and an attempt is made to connect to AWS, then a basic file is automatically created by the SDK or tool in a folder named .aws placed in your home/user directory.

ℹ Note: You need to sign in to your AWS access portal for your non-administrative user to retrieve values required as input during configuration, specifically for the sso-session section. Follow this guide for more details.

  • [Interactive] Configure your profile with the AWS CLI.
# confirm the aws-cli installation.
➜ aws --version

# start the interactive wizard to generate a config file.
➜ aws configure sso
  1. Connect to AWS with the AWS Toolkit Vs Code extension.
  • Verify your config file from the above step, [4].
➜ cat ~/.aws/config
  • Sign in using the Vs Code command pallete.
    To log in, launch the command palette Shift+Command+P (Ctrl+Shift+P Windows) then run the AWS: Add a New Connection command or from the AWS Explorer menu and choose the profile you configured. Your default browser will launch and bring you to the AWS access portal you assigned in the sso_start_url property.

ℹ If you previously signed in, for example, using the AWS CLI directly - aws sso login --profile default, you still need to add a new connection for Vs Code, after authentication you can select the profile default if it reads "Expired or invalid, select to authenticate" to authenticate your SDK tools under that existing session.