Here is some information about this architecture.
Here are the steps you can follow to build this solution on your own.
This lesson provides an in-depth look at how to use AWS Lambda to trigger Amazon SNS notifications. By linking serverless computing with notification services, you'll create a responsive system capable of alerting users or other services when specific conditions are met. It's a core component of many modern, event-driven architectures.
If you're using the Skillmix Labs feature, open the lab settings (the beaker icon) on the right side of the code editor. Then, click the Start Lab button to start hte lab environment.
Wait for the credentials to load. Then run this in the terminal.
Be sure to enter in your own access key and secret key and name your profile 'smx-lab'.
$ aws configure --profile smx-lab
AWS Access Key ID [None]:
AWS Secret Access Key [None]:
Default region name [None]: us-west-2
Default output format [None]:
Note: If you're using your own AWS account you'll need to ensure that you've created and configured a named AWS CLI profile named smx-lab.
Next, we'll create the required_providers
config. This config is used to specify the providers that are required for our Terraform configuration. In this case, we are requiring the aws
provider from HashiCorp with a version constraint of ~> 4.22
.
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 4.22"
}
}
required_version = ">= 0.14.9"
}
Next, we'll create the aws config which is used to configure the AWS provider in Terraform.
provider "aws" {
profile = "smx-lab"
region = "us-west-2"
}
Next, we'll create the archive_file config. This config is used to create a zip file from the source file specified, which in this case is ${path.module}/src/app.js
. The output of this config will be stored in ${path.module}/lambda.zip
.
data "archive_file" "lambda_zip_file" {
type = "zip"
source_file = "${path.module}/src/app.js"
output_path = "${path.module}/lambda.zip"
}
Next, we'll create the aws_iam_policy
config. This config is used to define an IAM policy for the lambda_basic_execution_role_policy
with the name AWSLambdaBasicExecutionRole
.
data "aws_iam_policy" "lambda_basic_execution_role_policy" {
name = "AWSLambdaBasicExecutionRole"
}
Next, we'll create the aws_iam_policy_document
config. This config is used to define an IAM policy document for a Lambda function.
data "aws_iam_policy_document" "lambda_policy_document" {
statement {
effect = "Allow"
actions = ["sns:Publish"]
resources = ["${aws_sns_topic.sns_topic.arn}"]
}
}
Next, we'll create the aws_sns_topic
config. This config is used to create an Amazon Simple Notification Service (SNS) topic.
resource "aws_sns_topic" "sns_topic" {
// configuration options go here
}
Next, we'll create the aws_lambda_function
config. This config is used to define an AWS Lambda function.
resource "aws_lambda_function" "lambda_function" {
function_name = "TopicPublisherFunction"
filename = "${data.archive_file.lambda_zip_file.output_path}"
source_code_hash = "${data.archive_file.lambda_zip_file.output_base64sha256}"
handler = "app.handler"
role = "${aws_iam_role.lambda_iam_role.arn}"
runtime = "nodejs14.x"
environment {
variables = {
SNStopic = "${aws_sns_topic.sns_topic.arn}"
}
}
}
Next, we'll create the aws_iam_role config. This config is used to create an AWS IAM role for a Lambda function.
resource "aws_iam_role" "lambda_iam_role" {
name_prefix = "LambdaSNSRole-"
managed_policy_arns = ["${data.aws_iam_policy.lambda_basic_execution_role_policy.arn}", "${aws_iam_policy.lambda_policy.arn}"]
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}
EOF
}
Next, we'll create the aws_iam_policy
config. This config is used to define an IAM policy in AWS.
resource "aws_iam_policy" "lambda_policy" {
name_prefix = "lambda_policy-"
path = "/"
policy = "${data.aws_iam_policy_document.lambda_policy_document.json}"
lifecycle {
create_before_destroy = true
}
}
Next, we'll create the TopicPublisherFunction config. This config is used to define the output value for the TopicPublisherFunction
function name.
output {
TopicPublisherFunction = {
value = "${aws_lambda_function.lambda_function.arn}"
description = "TopicPublisherFunction function name"
}
}
Next, we'll create the SNStopicARN config. This config is used to output the ARN (Amazon Resource Name) of an AWS SNS (Simple Notification Service) topic.
output {
SNStopicARN = {
value = "${aws_sns_topic.sns_topic.arn}"
description = "SNS topic ARN"
}
}
Deploy the Solution
Let's deploy this thing! If you haven't done so, start the Skillmix lab session and get the account credentials. Configure your Terraform environment to use those credentials.
Then, open a terminal or command prompt, navigate to the folder with your Terraform file, and execute these commands:
# initiatlize the project
$ terraform init
# show the plan
$ terraform plan
# apply the changes
$ terraform apply
Wait for the changes to be applied before proceeding.
Test the Solution
Use the AWS CLI to invoke the Lambda function. The function name is in the outputs of the Terraform deployment (the key is TopicPublisherFunction
):
Invoke the Lambda function to publish a message to SNS:
aws lambda invoke --function-name ENTER_YOUR_FUNCTION_NAME response.json