
Terraform is a commonly used IaC (Infrastructure as Code) tool for organizations. For Java-based Lambda functions there are no major differences on how to provision a Lambda function compared to other runtimes, however take extra while activating SnapStart for Lambda functions.
As mentioned earlier in the Lambda SnapStart section, SnapStart works with a Lambda version or alias instead of the
$latest version of function.
resource "aws_lambda_function" "UnicornStockBrokerFunction" {
function_name = "UnicornStockBrokerFunction"
filename = var.SourceCodePath
handler = "org.springframework.cloud.function.adapter.aws.FunctionInvoker::handleRequest"
runtime = "java11"
environment {
variables = {
TABLE_NAME = aws_dynamodb_table.transactions_table.name
BUCKET_NAME = aws_s3_bucket.validationfiles-bucket.id
JAVA_TOOL_OPTIONS = "-XX:+TieredCompilation -XX:TieredStopAtLevel=1"
}
}
source_code_hash = filebase64sha256(var.SourceCodePath)
role = aws_iam_role.UnicornStockBrokerFunctionRole.arn
timeout = "200"
memory_size = "2048"
snap_start {
apply_on ="PublishedVersions"
}
publish = true
}
resource "aws_lambda_alias" "UnicornStockBrokerFunction_SnapStartAlias" {
name = "SnapStart"
description = "Alias for SnapStart"
function_name = aws_lambda_function.UnicornStockBrokerFunction.function_name
function_version = aws_lambda_function.UnicornStockBrokerFunction.version
}
resource "aws_api_gateway_integration" "createproduct-lambda" {
rest_api_id = aws_api_gateway_rest_api.unicornstockbroker_apigw.id
resource_id = aws_api_gateway_method.createtransactions.resource_id
http_method = aws_api_gateway_method.createtransactions.http_method
integration_http_method = "POST"
type = "AWS_PROXY"
uri = aws_lambda_alias.UnicornStockBrokerFunction_SnapStartAlias.invoke_arn
}
One of the many benefits of using AWS SAM CLI is its ability to facilitate local development and testing. The core Terraform workflow consists of three stages: Write, Plan, and Apply. With AWS SAM CLI support for Terraform, you can take advantage of the AWS SAM CLI sam local set of commands while continuing to use your Terraform workflows to manage your applications on AWS.
Refer to AWS SAM CLI Terraform support to learn more.