Access SSM Parameters In Lambda Function (Boto3)

2023.04.04

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

Hello, This is Shiwani Lawte from the DA Business Headquarters.

Today, we will learn how to access SSM parameters in AWS Lambda function using Boto3.

What is SSM

Parameter Store, a capability of AWS Systems Manager, provides secure, hierarchical storage for configuration data management and secrets management. You can store data such as passwords, database strings, Amazon Machine Image (AMI) IDs, and license codes as parameter values.

We can access parameters from Parameter Store in AWS Lambda functions without using an SDK. AWS Parameters and Secrets Lambda Extension can be use to retrieves parameter values and caches them for future use. Using the Lambda extension can reduce your costs by reducing the number of API calls to Parameter Store.

Follow these steps to create and access Parameters from Lambda function

  • Create Parameter in SSM
  • Create New IAM permission for accessing SSM parameters(Variable) from Lambda function
  • Create Lambda function: Use Boto3 to read/update SSM parameters

Create Parameter in SSM

  1. Go to AWS console and search System Manager

  2. Click Create Parameter

  3. Create a new variable: /my-param/dev/userName Value : Enter a value which you want to define in parameter. Eg: Param@1234

Create New IAM Role

To access SSM parameter from lambda need to give IAM Permission.

  1. Go to IAM Console =>IAM => Roles => Create Role

2. Select AWS services: Choose Lambda 3. Select Permission: AmazonSSMFullAccess 4. Enter Role Name => Create Role: lambda_ssm_access

The role has been created. We will use this permission in lambda function to access AWS SSM parameters.

Create Lambda Function : Use Boto3 to read SSM parameters

  1. Go to Services =>Lambda => Create Function => Enter Function Name : Access-SSM-Parameter
  2. Go to permissions => And select Existing IAM Role: lambda_ssm_access
  3. Lambda function is created
  4. Write function code to access Stored parameter

Execution Result :

Conclusion

Environment variables may occasionally need to be encrypted and stored somewhere where number of lambda functions can access them. With the help of the "Parameter Store" feature offered by AWS SSM, you can conveniently store your parameters in SSM and encrypt them by KMS.

References:

  1. What is AWS Systems Manager?
  2. Boto3/SSM Client