[ad_1]
To create an AWS Lambda perform utilizing Terraform, it’s essential to outline the required sources in a Terraform configuration file. Right here’s an instance of how one can create a Lambda perform utilizing Terraform:
Possibility 1 – Seperate Lambda Supply
- Create a brand new listing to your Terraform configuration and navigate to it in your terminal.
- Create a brand new file with a
.tfextension, comparable tolambda.tf, and open it in a textual content editor. - Within the lambda.tf file, add the next code:
supplier "aws" {
area = "us-east-1" # Substitute together with your desired AWS area
}
useful resource "aws_lambda_function" "my_lambda" {
function_name = "my-lambda-function"
position = aws_iam_role.lambda_role.arn
handler = "index.handler"
runtime = "nodejs14.x" # Substitute together with your desired runtime
// Substitute with the trail to your Lambda perform code
filename = "path/to/lambda/code.zip"
// Substitute with the suitable values to your Lambda perform
surroundings {
variables = {
KEY = "VALUE"
KEY2 = "VALUE2"
}
}
}
useful resource "aws_iam_role" "lambda_role" {
identify = "my-lambda-role"
assume_role_policy = <<EOF
{
"Model": "2012-10-17",
"Assertion": [
{
"Effect": "Allow",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
EOF
}
useful resource "aws_iam_role_policy_attachment" "lambda_policy_attachment" {
position = aws_iam_role.lambda_role.identify
policy_arn = "arn:aws:iam::aws:coverage/service-role/AWSLambdaBasicExecutionRole"
}
- Within the above code, you possibly can customise the next components:
area: Specify the AWS area the place you wish to create the Lambda perform.runtime: Specify the runtime surroundings to your Lambda perform (e.g.,nodejs14.x,python3.8, and so on.).filename: Replace the trail to your Lambda perform code. Make sure that the code is packaged in a ZIP file.
You can too modify the surroundings variables part (KEY and VALUE) in line with your necessities.
-
Save the
lambda.tffile. -
Initialize the Terraform configuration by working the next command in your terminal:
terraform init
- As soon as the initialization is full, you possibly can create the Lambda perform by working the next command:
terraform apply
Terraform will analyze the configuration, immediate for affirmation, after which create the required sources, together with the Lambda perform.
Possibility 2 – Inline Lambda Supply
An inline Lambda perform signifies that the perform code is outlined immediately within the Terraform configuration file as a substitute of referencing an exterior code file. Right here’s an instance of how one can create an inline Lambda perform utilizing Terraform:
- Comply with steps 1 and a couple of from the earlier reply to arrange your Terraform configuration.
- Within the
lambda.tffile, add the next code:
supplier "aws" {
area = "us-east-1" # Substitute together with your desired AWS area
}
useful resource "aws_lambda_function" "my_lambda" {
function_name = "my-lambda-function"
position = aws_iam_role.lambda_role.arn
handler = "index.handler"
runtime = "nodejs14.x" # Substitute together with your desired runtime
// Outline the inline Lambda perform code
inline_code = <<-EOT
exports.handler = async perform(occasion, context) {
console.log("Howdy, inline Lambda perform!");
return {
statusCode: 200,
physique: "Howdy from inline Lambda!"
};
};
EOT
// Substitute with the suitable values to your Lambda perform
surroundings {
variables = {
KEY = "VALUE"
KEY2 = "VALUE2"
}
}
}
useful resource "aws_iam_role" "lambda_role" {
identify = "my-lambda-role"
assume_role_policy = <<EOF
{
"Model": "2012-10-17",
"Assertion": [
{
"Effect": "Allow",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
EOF
}
useful resource "aws_iam_role_policy_attachment" "lambda_policy_attachment" {
position = aws_iam_role.lambda_role.identify
policy_arn = "arn:aws:iam::aws:coverage/service-role/AWSLambdaBasicExecutionRole"
}
- Customise the code as wanted, such because the
area,function_name,runtime, and surroundings variables. - Save the
lambda.tffile. - Initialize the Terraform configuration by working
terraform initin your terminal. - As soon as the initialization is full, create the Lambda perform by working
terraform apply.
Terraform will analyze the configuration, immediate for affirmation, and create the inline Lambda perform utilizing the code offered within the inline_code block.
That’s it! You have got now created an inline Lambda perform utilizing Terraform.
[ad_2]
