Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try S3 notification #27

Merged
merged 3 commits into from
Sep 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cloud_environments/terraform.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ TF_VAR_slack_signing_secret=${slack_signing_secret}
TF_VAR_slack_post_webhook_url=${slack_post_webhook_url}
set +o allexport


# below is just for testing golang build! The actual build command is in terraform lambda module `command` property
if (
cd $GOLANG_SRC_DIR && \
go build ./cmd/landing && \
Expand Down
5 changes: 5 additions & 0 deletions cloud_module/global_ssm.tf
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
data aws_ssm_parameter newssite_economy {
name = "/app/media-literacy/newssites/ECONOMY"
}

locals {
newssite_economy_tokens = split(",", data.aws_ssm_parameter.newssite_economy.value)
newssite_economy_alias = local.newssite_economy_tokens[2]
}
9 changes: 8 additions & 1 deletion cloud_module/lambda.tf
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ module "scraper_lambda" {
# Based on golang https://github.com/snsinfu/terraform-lambda-example/blob/master/Makefile#L23
source_path = [{
path = "${path.module}/../lambda_golang/"
commands = ["go build ./cmd/landing", ":zip"]
commands = ["${local.go_build_flags} go build ./cmd/landing", ":zip"]
patterns = ["landing"]
}]

Expand Down Expand Up @@ -104,3 +104,10 @@ module "scraper_lambda" {
Project = local.project_name
}
}

locals {
# amd64 is the x86 instruction set
# arm is not (like M1), not supported by AWS lambda go runtime yet
# https://stackoverflow.com/questions/26951940/how-do-i-make-go-get-to-build-against-x86-64-instead-of-i386
go_build_flags = "GOOS=linux GOARCH=amd64 CGO_ENABLED=0 "
}
20 changes: 20 additions & 0 deletions cloud_module/landing_s3_trigger.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
resource "aws_lambda_permission" "allow_bucket" {
statement_id = "AllowExecutionFromS3Bucket"
action = "lambda:InvokeFunction"
function_name = module.batch_stories_fetch_parse_lambda.lambda_function_arn
principal = "s3.amazonaws.com"
source_arn = data.aws_s3_bucket.archive.arn
}

resource "aws_s3_bucket_notification" "bucket_notification" {
bucket = data.aws_s3_bucket.archive.id

lambda_function {
lambda_function_arn = module.batch_stories_fetch_parse_lambda.lambda_function_arn
events = ["s3:ObjectCreated:*"]
filter_prefix = "${local.newssite_economy_alias}/"
filter_suffix = ".html"
}

depends_on = [aws_lambda_permission.allow_bucket]
}
4 changes: 2 additions & 2 deletions cloud_module/stories_sfn.tf
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ module batch_stories_fetch_parse_lambda {
function_name = "${local.project_name}-batch-stories-fetch-parse"
description = "Batch fetch and parse all stories of a landing page"
handler = "stories"
runtime = "go1.x"
runtime = "go1.x"

source_path = [{
path = "${path.module}/../lambda_golang/"
commands = ["go build ./cmd/stories", ":zip"]
commands = ["${local.go_build_flags} go build ./cmd/stories", ":zip"]
patterns = ["stories"]
}]

Expand Down
2 changes: 1 addition & 1 deletion cloud_module/stories_sqs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ module "stories_queue_consumer_lambda" {
runtime = "go1.x"
source_path = [{
path = "${path.module}/../lambda_golang/"
commands = ["go build ./cmd/story", ":zip"]
commands = ["${local.go_build_flags} go build ./cmd/story", ":zip"]
patterns = ["story"]
}]
publish = true
Expand Down
29 changes: 20 additions & 9 deletions lambda_golang/cmd/stories/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,20 @@ package main
import (
"context"
"fmt"
"strings"
"math"
//"strings"
//"math"

"github.com/aws/aws-lambda-go/lambda"
"github.com/aws/aws-lambda-go/events" // https://github.com/aws/aws-lambda-go/blob/main/events/README_S3.md
"github.com/rivernews/GoTools"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/sqs"
//"github.com/aws/aws-sdk-go-v2/aws"
//"github.com/aws/aws-sdk-go-v2/config"
//"github.com/aws/aws-sdk-go-v2/service/sqs"

// local packages
"github.com/rivernews/media-literacy/pkg/cloud"
"github.com/rivernews/media-literacy/pkg/newssite"
//"github.com/rivernews/media-literacy/pkg/cloud"
//"github.com/rivernews/media-literacy/pkg/newssite"
)


Expand All @@ -34,9 +35,17 @@ type LambdaResponse struct {
Message string `json:"message:"`
}

func HandleRequest(ctx context.Context, stepFunctionInput StepFunctionInput) (LambdaResponse, error) {
GoTools.Logger("INFO", fmt.Sprintf("Batch stories lambda started! Landing page S3 path: `%s`; going to test delayed messages...", stepFunctionInput.LandingS3Key))
func HandleRequest(ctx context.Context, s3Event events.S3Event) (LambdaResponse, error) {
// GoTools.Logger("INFO", fmt.Sprintf("Batch stories lambda started! Landing page S3 path: `%s`; going to test delayed messages...", stepFunctionInput.LandingS3Key))
GoTools.SendSlackMessage("Ha ha!")
GoTools.SendSlackMessage(fmt.Sprintf("Hello! ``` %v ```", s3Event))

return LambdaResponse{
OK: true,
Message: "Done",
}, nil

/*
landingPageHtmlText := cloud.Pull(stepFunctionInput.LandingS3Key)

stories := newssite.GetStoriesFromEconomy(landingPageHtmlText)
Expand Down Expand Up @@ -100,4 +109,6 @@ func HandleRequest(ctx context.Context, stepFunctionInput StepFunctionInput) (La
OK: true,
Message: fmt.Sprintf("Sent %d messages OK", len(linkChunks)),
}, nil

*/
}