Adding a new stage with a Lambda function to pass a parameter from CodePipeline to the function.

AWS CodePipeline Lambda function parameter passing methods.

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 AWS CodePipeline to a Lambda function, there are multiple methods available. However, the best method depends on the requirements of the pipeline and the use case. In this scenario, the requirement is to pass a valid URL as a parameter to the Lambda function. Let's discuss each option provided in the answers:

Option 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 method involves passing the parameter as user parameters in the CodePipeline stage. The user parameters can be used to pass data between stages or invoke AWS Lambda functions. In this method, the pipeline sends the user parameters as a JSON event to the Lambda function, and the Lambda function retrieves the parameter value from the JSON event. The Lambda function can access the JSON event using the event object. Example: If the user parameter name is "MyParameter", the Lambda function can retrieve its value using the following code:

less
import json def lambda_handler(event, context): my_parameter = event["CodePipeline.job"]["data"]["actionConfiguration"]["configuration"]["UserParameters"] my_parameter_value = json.loads(my_parameter)["MyParameter"] # Use my_parameter_value in the Lambda function code

Option 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 method involves passing the parameter as input parameters to a CloudFormation stack launched by the Lambda function. The CodePipeline invokes the Lambda function, and the Lambda function launches the CloudFormation stack with the required input parameters. The CloudFormation stack can access the input parameters using the "!Ref" function. The Lambda function can then use the CloudFormation stack output values as required. Example: If the CloudFormation stack input parameter name is "MyParameter", the CloudFormation stack template can access its value using the following code:

yaml
Parameters: MyParameter: Type: String Resources: MyResource: Type: AWS::Lambda::Function Properties: Environment: Variables: MyParameter: !Ref MyParameter ...

Option 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 method involves passing the parameter as environment variables to the Lambda function. The CodePipeline stage can add the parameter as an environment variable, and the Lambda function can retrieve the environment variable value using the os.environ object. Example: If the environment variable name is "MyParameter", the Lambda function can retrieve its value using the following code:

python
import os def lambda_handler(event, context): my_parameter_value = os.environ["MyParameter"] # Use my_parameter_value in the Lambda function code

Option D: Create an API Gateway that passes a query string to the AWS Lambda function. Configure the CodePipeline stage to invoke the API Gateway. This method involves passing the parameter as a query string to an API Gateway, which in turn invokes the Lambda function. The CodePipeline stage can invoke the API Gateway with the required query string parameter. The Lambda function can retrieve the query string parameter value using the event object. Example: If the query string parameter name is "MyParameter", the Lambda function can retrieve its value using the following code:

python
def lambda_handler(event, context): my_parameter_value = event["queryStringParameters"]["MyParameter"] # Use my_parameter_value in the Lambda function code

In summary, for passing a parameter to a Lambda function in a