Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Amazon Lambda - Alias specific environment variables

I am using AWS Lambda and can use Alias feature to point to multiple code promotion stages that we have (e.g. dev, qa, prod etc). I have setup the alias the same name as stages. Most of these functions gets triggered from S3 or SNS which has a different instance for each stage.

How can I setup a alias based environment variable so the function can get the specific info. The env vars setup in the base function(typically dev) gets carried over to all alias which does not work for deployment.

I know how to use stage variables in API gateway but the current use is not via gateway.

like image 238
psuhas Avatar asked Feb 05 '17 16:02

psuhas


People also ask

Does Lambda support for environment variables?

Short description. You can't change the configuration (which includes environment variables) or function code in a published Lambda function version.

How do you point Lambda alias?

Managing aliases with the Lambda API To create an alias using the AWS Command Line Interface (AWS CLI), use the create-alias command. To change an alias to point a new version of the function, use the update-alias command. To delete an alias, use the delete-alias command.

How do you name environment variables?

Environment variable names used by the utilities in the Shell and Utilities volume of IEEE Std 1003.1-2001 consist solely of uppercase letters, digits, and the '_' (underscore) from the characters defined in Portable Character Set and do not begin with a digit.

How do I change the environment variables in AWS Lambda?

To change environment variables in the $LATEST version of your Lambda function, use one of the following: The Lambda console. The update-function-configuration command in the AWS CLI. Lambda API calls in the AWS SDKs.

What does your lambda function alias point to?

Your Lambda function alias now points to the latest function version that's configured with the environment variables that you want to use.

Why am I getting errors when using AWS Lambda?

Note: If you receive errors when running AWS Command Line Interface (AWS CLI) commands, make sure that you’re using the most recent AWS CLI version. Follow the instructions in Using AWS Lambda environment variables. Follow the instructions in update-function-configuration (AWS CLI command reference).

What is the Arn of a Lambda alias?

Each Lambda alias has a unique ARN and can be reconfigured once created to point to new versions of your function. Note: If you receive errors when running AWS Command Line Interface (AWS CLI) commands, make sure that you’re using the most recent AWS CLI version.


4 Answers

I don't believe there is a way to achieve what you are trying to. You would need to publish three versions of your Lambda function each with the correct environment variables and point each of your aliases to the correct version of the function.

You could use the description fields to help describe the versions before you point the aliases to them too, to make the changes more understandable.

like image 151
Aaron Williams Avatar answered Sep 19 '22 02:09

Aaron Williams


I also find it interesting this isn't part of the plan for aliases, however you do have the context available in your code - Context.InvokedFunctionArn

I think the MINDSET is that you may call, for example, a S3 bucket and have a prefix of TEST or DEV or PROD (based on context InvokedFunctionArn you know which alias). Given this context and security based on the ARN you can use bucket policy / IAM to restrict your TEST ARN can only reach TEST s3 prefix files. That solves security between environments.

NOTE: I disagree with this model and think environment variables should be in he aliases, and if not specified in the alias fall back to what is in the version.

Although this works, the complexity around extra conditions on prefix, etc. is something many times will be misconfigured - Having a separate bucket seems much safer and matches the Serverless Application Model documentation better.

like image 28
Steve Radich-BitShop.com Avatar answered Sep 22 '22 02:09

Steve Radich-BitShop.com


EDIT I'll leave this answer here as it may help some people but note that I found AspNetCoreStartupMode.FirstRequest caused longer cold starts by a few seconds.


I added some code to the LambdaEntryPoint to get the alias at Startup which means you can use it to load environment specific config:

public class LambdaEntryPoint : Amazon.Lambda.AspNetCoreServer.APIGatewayProxyFunction
{
    private ILambdaContext LambdaContext;

    public LambdaEntryPoint()
        : base(AspNetCoreStartupMode.FirstRequest)
    {

    }

    [LambdaSerializer(typeof(Amazon.Lambda.Serialization.Json.JsonSerializer))]
    public override async Task<APIGatewayProxyResponse> FunctionHandlerAsync(APIGatewayProxyRequest request, ILambdaContext lambdaContext)
    {
        LambdaContext = lambdaContext;

        return await base.FunctionHandlerAsync(request, lambdaContext);
    }

    protected override void Init(IWebHostBuilder builder)
    {
        var alias = LambdaContext?.InvokedFunctionArn.Substring(LambdaContext.InvokedFunctionArn.LastIndexOf(":") + 1);

        // Do stuff based on the environment

        builder
            .ConfigureAppConfiguration((hostingContext, config) =>
            {
                config.AddJsonFile($"appsettings.{alias}.json", optional: true, reloadOnChange: true);
            })
            .UseStartup<Startup>();
    }
}

I've added a gist here: https://gist.github.com/secretorange/710375bc62bbc1f32e05822f55d4e8d3

like image 45
Lee Gunn Avatar answered Sep 19 '22 02:09

Lee Gunn


The lambda context has invoked_function_arn – The Amazon Resource Name (ARN) that's used to invoke the function. Indicates if the invoker specified a version number or alias.

You can then use the alias to find the variables via Systems Manager parameter store, instead of environment variables.

like image 33
DLT Avatar answered Sep 21 '22 02:09

DLT