View a markdown version of this page

Lambda function example for the Parameter Store extension in AWS Systems Manager Cloud Extensions - AWS Systems Manager

Lambda function example for the Parameter Store extension in AWS Systems Manager Cloud Extensions

The following example in Python reads parameters through the extension in an AWS Lambda function. It uses the SSM client of the AWS SDK for Python (Boto3), which the Lambda Python runtime includes. The client uses the extension as its endpoint and the extension key as its credentials.

Before you begin, complete the following tasks:

  • Install the Cloud Extensions runtime as a layer of your function. For the steps, see Install for Lambda functions. In Lambda, the runtime runs the default profile of your account.

  • Add the Parameter Store extension to that profile, and approve the parameterStoreRead capability. For the steps, see Adding the extension to a profile.

  • Make sure that the IAM execution role of the extension allows ssm:GetParameter and ssm:GetParameters on the parameters. Also make sure that the role allows kms:Decrypt on the key that encrypts the SecureString parameters. For the policy, see Required IAM permissions.

  • Make the extension key of the profile available to the function in the environment variables EXTENSION_ACCESS_KEY_ID and EXTENSION_SECRET_KEY.

The function itself needs no Parameter Store permissions. The extension calls Parameter Store with the execution role of the extension. The function needs only the permissions to write logs, which the AWSLambdaBasicExecutionRole managed policy provides.

import os import boto3 from botocore.config import Config # The extension serves the SSM API of Parameter Store on the runtime's local port. ENDPOINT_URL = "http://localhost:5333/parameterstore" # Sign the requests with the extension key of the profile, not with the credentials of the function. ssm = boto3.client( "ssm", endpoint_url=ENDPOINT_URL, region_name=os.environ["AWS_REGION"], aws_access_key_id=os.environ["EXTENSION_ACCESS_KEY_ID"], aws_secret_access_key=os.environ["EXTENSION_SECRET_KEY"], config=Config(retries={"mode": "standard"}), ) def lambda_handler(event, context): # GetParameter: one parameter db_host = ssm.get_parameter(Name="/my-app/db-host")["Parameter"]["Value"] # GetParameter with decryption: a SecureString parameter db_password = ssm.get_parameter(Name="/my-app/db-password", WithDecryption=True)["Parameter"] # GetParameters: up to 10 parameters in one request batch = ssm.get_parameters(Names=["/my-app/db-host", "/my-app/api-url", "/my-app/does-not-exist"]) return { "dbHost": db_host, "dbPasswordVersion": db_password["Version"], # never return or log a decrypted value "batch": {p["Name"]: p["Value"] for p in batch["Parameters"]}, "invalidParameters": batch["InvalidParameters"], }

The code works as follows:

  • The client is the standard SSM client of Boto3. Only endpoint_url and the credentials differ from a client that calls Parameter Store.

  • The handler reads /my-app/db-host with GetParameter and /my-app/db-password with GetParameter and decryption. It reads three names with GetParameters, one of which doesn't exist.

  • When the extension returns an error, Boto3 raises it as it does for Parameter Store, for example ssm.exceptions.ParameterNotFound. The code and message are in error.response["Error"].

The function returns output similar to the following:

{ "dbHost": "db.example.com", "dbPasswordVersion": 1, "batch": { "/my-app/api-url": "https://api.example.com", "/my-app/db-host": "db.example.com" }, "invalidParameters": [ "/my-app/does-not-exist" ] }

If the runtime isn't running in the function, the invocation fails with an EndpointConnectionError. Check that the layer is attached and that the runtime started. For the log messages, see Install for Lambda functions.

Each Lambda execution environment runs its own copy of the runtime and its own cache.