ANGADJAVA Code Samples Bytes,Dev Blog Deploy an AWS Lambda function in Go with Terraform

Deploy an AWS Lambda function in Go with Terraform

0 Comments 7:06 pm

neuburg on the danube, www, aw-air image-2629971.jpg

Terraform is an open-source infrastructure as code software tool that allows you to define and provision infrastructure resources in a safe and repeatable way. In this tutorial, we will guide you on how to use Terraform with Go Lang and AWS Lambda Function to create a real-time example.

Prerequisites

Before we begin, make sure that you have the following installed on your machine:

Creating a Go Lang Function

To create a Go Lang function, follow these steps:

  1. Open a terminal window and create a new directory for your Go Lang function.
  2. Create a new file named main.go in the directory.
  3. Add the following code to the main.go file:
package main 
import (    "fmt"
    "context"
		"github.com/aws/aws-lambda-go/lambda")
		
		func Handler(ctx context.Context, name string) (string, error)
		 { 
			   return fmt.Sprintf("Hello, %s!", name), nil
				 
			}
			
			func main() {
					    lambda.Start(Handler)
				}

In this example, we are creating a simple Lambda function that takes a name as input and returns a greeting message.

  1. Save the main.go file.

Building and Deploying the Go Lang Function

Once you have created your Go Lang function, you can build and deploy it using the following steps:

  1. Open a terminal window and navigate to the directory where your main.go file is located.
  2. Run the following command to build your Go Lang function:
GOOS=linux GOARCH=amd64 go build -o main main.go
  1. Run the following command to create a ZIP file of your Go Lang function:
zip deployment.zip main
  1. Run the following command to deploy your Go Lang function to AWS Lambda:
aws lambda create-function --function-name my-function --runtime go1.x --handler main --zip-file fileb://./deployment.zip --role arn:aws:iam::123456789012:role/lambda-role

In this example, we are deploying the function with the name my-function, using the go1.x runtime, and assigning the lambda-role IAM role.

Creating a Terraform Configuration

Once you have created and deployed your Go Lang function, you can create a Terraform configuration using the following steps:

  1. Create a new directory for your Terraform configuration.
  2. Create a new file named main.tf in the directory.
  3. Add the following code to the main.tf file:
provider "aws" {  region = "us-west-2"}resource "aws_lambda_function" "example" {  function_name = "my-function"  handler = "main"  runtime = "go1.x"  filename = "deployment.zip"  role = "arn:aws:iam::123456789012:role/lambda-role"}

In this example, we are using the aws_lambda_function resource to create a new Lambda function with the name my-function, the go1.x runtime, and the deployment.zip file.

  1. Save the main.tf file.

Initializing and Applying the Terraform Configuration

Once you have created your Terraform configuration, you can initialize and apply it using the following steps:

  1. Open a terminal window and navigate to the directory where your main.tf file is located.
  2. Run the following command to initialize Terraform:
terraform init
  1. Run the following command to apply your Terraform configuration:
terraform apply
  1. When prompted, review the changes that Terraform will make to your infrastructure and type yes to apply the changes.
  2. Wait for Terraform to provision the new Lambda function.

Testing the Go Lang Function

Once you have provisioned your Lambda function, you can test it using the following steps:

  1. Open a terminal window and run the following command to invoke your Lambda function:
aws lambda invoke --function-name my-function --payload '{"name": "Terraform"}' output.txt
  1. Run the following command to view the output of your Lambda function:
cat output.txt

You should see the following output:

"Hello, Terraform!"

Conclusion

Congratulations! You have successfully used Terraform with Go Lang and AWS Lambda Function to create a real-time example. You can now use Terraform to define and provision infrastructure resources in a safe and repeatable way, and use Go Lang to create powerful Lambda functions that can be deployed to AWS.

Leave a Reply

Your email address will not be published. Required fields are marked *