Skip to content

Instantly share code, notes, and snippets.

View manavskohli's full-sized avatar

Manav Kohli manavskohli

  • San Francisco, CA
View GitHub Profile
@manavskohli
manavskohli / sns.tf
Last active February 14, 2019 18:36
# ...
resource "aws_sns_topic_subscription" "topic_lambda_sync_bucket_1" {
topic_arn = "${aws_sns_topic.s3_fanout.arn}"
protocol = "lambda"
endpoint = "${aws_lambda_function.source_to_target_1_sync.arn}"
}
resource "aws_sns_topic_subscription" "topic_lambda_sync_bucket_2" {
topic_arn = "${aws_sns_topic.s3_fanout.arn}"
protocol = "lambda"
An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
+ create
<= read (data resources)
Terraform will perform the following actions:
<= data.aws_iam_policy_document.move_object
id: <computed>
json: <computed>
# ...
resource "aws_lambda_permission" "with_sns_1" {
statement_id = "AllowExecutionFromSNS"
action = "lambda:InvokeFunction"
function_name = "${aws_lambda_function.source_to_target_1_sync.function_name}"
principal = "sns.amazonaws.com"
source_arn = "${aws_sns_topic.s3_fanout.arn}"
}
resource "aws_lambda_permission" "with_sns_2" {
@manavskohli
manavskohli / iam.tf
Last active February 14, 2019 18:39
# The policy
data "aws_iam_policy_document" "move_object" {
statement {
effect = "Allow"
actions = [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
]
resources = [
resource "aws_lambda_function" "source_to_target_1_sync" {
function_name = "source_to_target_1_sync"
filename = "${data.archive_file.push_to_bucket_1.output_path}"
source_code_hash = "${data.archive_file.push_to_bucket_1.output_base64sha256}"
role = "${aws_iam_role.source_to_target_sync.arn}"
handler = "push_to_bucket_1.lambda_handler"
runtime = "python2.7"
}
resource "aws_lambda_function" "source_to_target_2_sync" {
data "archive_file" "push_to_bucket_1" {
type = "zip"
source_file = "push_to_bucket_1.py"
output_path = "push_to_bucket_1.zip"
}
data "archive_file" "push_to_bucket_2" {
type = "zip"
source_file = "push_to_bucket_2.py"
output_path = "push_to_bucket_2.zip"
# ...
provider "archive" {
version = "~> 1.1"
}
# Original source code:
# aws.amazon.com/blogs/compute/content-replication-using-aws-lambda-and-amazon-s3/
import urllib
import boto3
import ast
import json
print('Loading function')
def lambda_handler(event, context):
s3 = boto3.client('s3')
# Original source code:
# aws.amazon.com/blogs/compute/content-replication-using-aws-lambda-and-amazon-s3/
import urllib
import boto3
import ast
import json
print('Loading function')
def lambda_handler(event, context):
s3 = boto3.client('s3')
@manavskohli
manavskohli / s3.tf
Last active February 14, 2019 18:41
# ...
resource "aws_s3_bucket_notification" "object_create_sns" {
bucket = "${aws_s3_bucket.source-bucket.id}"
topic {
topic_arn = "${aws_sns_topic.s3_fanout.arn}"
events = ["s3:ObjectCreated:*"]
}
}