Adding a new stage to AWS CodePipeline for invoking a Lambda function with a parameter

Passing Parameters from CodePipeline to Lambda Function

Prev Question Next Question

Question

You have a pipeline configured in the AWS CodePipeline service.

You want to add a new stage that invokes a Lambda function to test the new build.

In this CodePipeline stage, a parameter needs to be passed to the Lambda function and its value should be a valid URL such as http://mytest.com.au.

Which of the following methods would you select to pass the parameter from CodePipeline to the Lambda function?

Answers

Explanations

Click on the arrows to vote for the correct answer

A. B. C. D.

Correct Answer - A.

Option A is CORRECT because you can configure the UserParameters in the CodePipeline stage where the Lambda function is invoked.

UserParameters is included in the JSON event sent to Lambda and can be fetched by the Lambda function.

Option B is incorrect because there is no need to launch a CloudFormation stack and the parameter cannot be fetched through the “!Ref” function.

Option C is incorrect because the parameter is included in the JSON event sent to Lambda by CodePipeline.

It is not an environment parameter for the Lambda function.

Option D is incorrect because an API Gateway is not required since CodePipeline can pass a parameter to Lambda.

Besides, API Gateway is not a supported service that CodePipeline can directly invoke.

Reference:

https://docs.aws.amazon.com/codepipeline/latest/userguide/actions-invoke-lambda-function.html.
Edit action

Action name
Choose a name for your action

No more than 100 characters

‘Action provider
AWS Lambda v|
Region
Asia Pacific (Sydney) v ]

Input artifacts
Choose an input artifact for this action, Learn more [2

Add

No more than 100 characters

Function name
Choose a function that you have already created in the AWS Lambda console. Or create a function in the Amazon Lambda console and then return to this task.

Q

This string will be used in the event data parameter passed to the handler in AWS Lambda.

Variable namespace - optional
Choose a namespace for the output variables from this action. You must choose @ namespace if you want to use the variables ths action produces in your configuration. Learn more (2

Output artifacts
Choose a name for the output ofthis action

‘Add

No more than 100 characters

To pass a parameter from CodePipeline to a Lambda function, there are multiple ways to achieve it, but the best approach depends on your specific use case. Here's a breakdown of the four options provided in the question:

A. Configure the parameter in the user parameters of the new CodePipeline stage. In the Lambda function, retrieve the parameter value from the JSON event that CodePipeline sends to Lambda.

This approach involves configuring the parameter in the user parameters of the new CodePipeline stage, and then retrieving the parameter value from the JSON event that CodePipeline sends to Lambda. This method is useful when the parameter value is specific to a particular stage in the pipeline.

B. Launch a CloudFormation stack in the Lambda function and the parameter is passed from CodePipeline to the CloudFormation stack through the “!Ref” function.

This approach involves launching a CloudFormation stack in the Lambda function, and then passing the parameter from CodePipeline to the CloudFormation stack using the "!Ref" function. This method is useful when the Lambda function needs to create or modify resources in the CloudFormation stack based on the parameter value.

C. Add the parameter in the new CodePipeline stage. In the Lambda function, the parameter becomes an environment parameter. For example, if the Lambda language is Python, the value can be retrieved through “os.environ[Parameter_Name]”.

This approach involves adding the parameter in the new CodePipeline stage, and then making it an environment parameter in the Lambda function. This method is useful when the parameter value is required by multiple functions or processes within the Lambda function.

D. Create an API Gateway that passes a querystring to the AWS Lambda function. Configure the CodePipeline stage to invoke the API Gateway.

This approach involves creating an API Gateway that passes a querystring to the Lambda function, and then configuring the CodePipeline stage to invoke the API Gateway. This method is useful when the Lambda function needs to be invoked by external systems or services.

In this scenario, Option A seems like the best choice, as it's the simplest and most direct method for passing a parameter from CodePipeline to a Lambda function. Option C could also be a valid choice if the parameter value needs to be accessed by multiple functions within the Lambda function. However, if the Lambda function needs to create or modify resources in a CloudFormation stack based on the parameter value, Option B might be a better choice. Option D seems like the least appropriate choice, as it adds unnecessary complexity to the pipeline and Lambda function.