Search code examples
aws-lambdaamazon-iamaws-lambda-layers

AccessDeniedException when retrieving AWS Parameters from Lambda


I am attempting to access system parameters from a Lambda developed using C#

I have added the required lambda layer as per https://docs.aws.amazon.com/systems-manager/latest/userguide/ps-integration-lambda-extensions.html#ps-integration-lambda-extensions-sample-commands

The lambda execution role has the following in the IAM definition (???????? replacing actual account id)

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "VisualEditor0",
            "Effect": "Allow",
            "Action": [
                "ssm:*"
            ],
            "Resource": "arn:aws:ssm:*:???????????:parameter/*"
        }
    ]
}

As per the AWS page reference above I made a HTTP GET request to http://localhost:2773/systemsmanager/parameters/get/?name=/ClinMod/SyncfusionKey&version=1

This is failing with the following response

{
    "Version": "1.1",
    "Content": {
        "Headers": [
            {
                "Key": "Content-Type",
                "Value": [
                    "text/plain"
                ]
            },
            {
                "Key": "Content-Length",
                "Value": [
                    "31"
                ]
            }
        ]
    },
    "StatusCode": 401,
    "ReasonPhrase": "Unauthorized",
    "Headers": [
        {
            "Key": "X-Amzn-Errortype",
            "Value": [
                "AccessDeniedException"
            ]
        },
        {
            "Key": "Date",
            "Value": [
                "Thu, 01 Dec 2022 12:16:59 GMT"
            ]
        }
    ],
    "TrailingHeaders": [],
    "RequestMessage": {
        "Version": "1.1",
        "VersionPolicy": 0,
        "Content": null,
        "Method": {
            "Method": "GET"
        },
        "RequestUri": "http://localhost:2773/systemsmanager/parameters/get/?name=/ClinMod/SyncfusionKey&version=1",
        "Headers": [],
        "Properties": {},
        "Options": {}
    },
    "IsSuccessStatusCode": false
}

Any clues where I am going wrong?


Solution

  • After a discussion on an AWS forum, it looks like the issue was that I was not setting a required header of the HTTP GET request.

    For this to work, it needs the X-Aws-Parameters-Secrets-Token header set with the AWS_SESSION_TOKEN.

    Once I did this it worked fine.