Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
867 views
in Technique[技术] by (71.8m points)

amazon web services - Lambda retrieve SSM parameter in Localstack

Using the latest localstack-full Docker image, I have stored an encrypted SSM parameter like this:

aws --region us-east-1 --endpoint-url=http://localhost:4566 ssm put-parameter --name "dbpassword" --value "secret2" --type "SecureString"

Then I have implemented a lambda in Go that I can access via API gateway. The implementation looks like this:

package main

import (
    "context"
    "github.com/aws/aws-lambda-go/events"
    "github.com/aws/aws-lambda-go/lambda"
    "github.com/aws/aws-sdk-go-v2/aws"
    "github.com/aws/aws-sdk-go-v2/config"
    "github.com/aws/aws-sdk-go-v2/service/ssm"
    "log"
    "os"
)

type Event struct {
    Name string `json:"name"`
}

func HandleRequest(ctx context.Context, event Event) (events.APIGatewayProxyResponse, error) {
    theAwsRegion := os.Getenv("AWS_REGION")

    //customResolver := aws.EndpointResolverFunc(func(service, region string) (aws.Endpoint, error) {
    //  return aws.Endpoint{
    //      PartitionID:   "aws",
    //      URL:           "localhost:4566",
    //      SigningRegion: "us-east-1",
    //  }, nil
    //})

    theConfig, err := config.LoadDefaultConfig(
        ctx,
        config.WithRegion(theAwsRegion),
        //config.WithEndpointResolver(customResolver),
    )
    if err != nil {
        log.Fatalf("Unable to load SDK config, %v", err)
    }

    ssmClient := ssm.NewFromConfig(theConfig)

    if ssmClient != nil {
        ssmOutput, err := ssmClient.GetParameter(ctx, &ssm.GetParameterInput{Name: aws.String("dbpassword"), WithDecryption: true})

        if err != nil {
            return events.APIGatewayProxyResponse{
                StatusCode:      200,
                Headers:         map[string]string{"Context-Type": "text/plain"},
                Body:            "Error occurred getting password",
                IsBase64Encoded: false,
            }, nil
        }

        thePassword := ssmOutput.Parameter.Value

        return events.APIGatewayProxyResponse{
            StatusCode:      200,
            Headers:         map[string]string{"Context-Type": "text/plain"},
            Body:            "Got a SSM client " + *thePassword,
            IsBase64Encoded: false,
        }, nil
    }

    return events.APIGatewayProxyResponse{
        StatusCode:      200,
        Headers:         map[string]string{"Context-Type": "text/plain"},
        Body:            "Failed to obtain SSM client",
        IsBase64Encoded: false,
    }, nil
}

func main() {
    lambda.Start(HandleRequest)
}

However, the lambda only responds with "{}" as soon as the call to GetParameter is introduced. An SSM client is successfully retrieved. There is also code commented-out attempt to use a custom endpoint resolver, which produces the same result. Is there a problem with Localstack or what am I doing wrong? Retrieving the parameter using the AWS CLI works without problems.

Update: I have tried to access the Localstack container from the lambda container over port 4566 and were able to obtain a response from the Localstack container.

question from:https://stackoverflow.com/questions/66047087/lambda-retrieve-ssm-parameter-in-localstack

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...