Skip to content

Commit 2be538e

Browse files
author
Kai Hendry
committed
Merge branch 'revert'
2 parents 5e38961 + b99d88e commit 2be538e

File tree

10 files changed

+197
-109
lines changed

10 files changed

+197
-109
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
lambda2sns

.travis.yml

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,31 @@
11
sudo: required
22
language: go
33
go:
4-
- tip
4+
- tip
55
before_install:
6-
- curl https://raw.githubusercontent.com/apex/apex/master/install.sh | sudo sh
6+
- pip install --user awscli
7+
- pip install --user aws-sam-cli
78
install:
8-
- go get -t ./...
9+
- go get -t ./...
910
deploy:
10-
# dev
11-
- provider: script
12-
script: make dev
13-
skip_cleanup: true
14-
on:
15-
branch: master
16-
# demo
17-
- provider: script
18-
script: AWS_ACCESS_KEY_ID=$AWS_915001051872_ID AWS_SECRET_ACCESS_KEY=$AWS_915001051872_SECRET make demo
19-
skip_cleanup: true
20-
on:
21-
branch: master
22-
# production
23-
- provider: script
24-
script: AWS_ACCESS_KEY_ID=$AWS_192458993663_ID AWS_SECRET_ACCESS_KEY=$AWS_192458993663_SECRET make prod
25-
skip_cleanup: true
26-
on:
11+
# dev
12+
- provider: script
13+
script: make deploy
14+
skip_cleanup: true
15+
on:
2716
branch: master
17+
# demo
18+
- provider: script
19+
script: AWS_ACCESS_KEY_ID=$AWS_915001051872_ID AWS_SECRET_ACCESS_KEY=$AWS_915001051872_SECRET make demo
20+
skip_cleanup: true
21+
on:
22+
branch: master
23+
# production
24+
- provider: script
25+
script: AWS_ACCESS_KEY_ID=$AWS_192458993663_ID AWS_SECRET_ACCESS_KEY=$AWS_192458993663_SECRET make prod
26+
skip_cleanup: true
27+
on:
28+
branch: master
2829

2930
env:
3031
- GO111MODULE=on

Makefile

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,33 @@
1-
all:
2-
@echo make {dev,demo,prod} to deploy
1+
PROJECT = $(shell basename $(CURDIR))
2+
STACK_NAME ?= $(PROJECT)
3+
AWS_REGION = ap-southeast-1
4+
DEPLOY_S3_BUCKET = dev-media-unee-t
5+
DEPLOY_S3_PREFIX = lambda2sns
36

4-
logs:
5-
apex -r ap-southeast-1 --env dev logs -f
7+
.PHONY: deps clean build test deploy
68

7-
dev:
8-
@echo $$AWS_ACCESS_KEY_ID
9-
apex -r ap-southeast-1 --env dev deploy
9+
# https://blog.deleu.dev/leveraging-aws-sqs-retry-mechanism-lambda/
1010

11-
demo:
12-
@echo $$AWS_ACCESS_KEY_ID
13-
apex -r ap-southeast-1 --env demo deploy
11+
deps:
12+
go mod tidy
1413

15-
prod:
16-
@echo $$AWS_ACCESS_KEY_ID
17-
apex -r ap-southeast-1 --env prod deploy
14+
build: deps
15+
GOOS=linux GOARCH=amd64 go build -o lambda2sns .
1816

19-
testdev:
20-
apex -r ap-southeast-1 --env dev invoke simple < event.json
17+
test:
18+
go test ./...
2119

22-
testdemo:
23-
apex -r ap-southeast-1 --env demo invoke simple < event.json
20+
logs:
21+
sam logs -n alambda_simple -t
2422

25-
testprod:
26-
apex -r ap-southeast-1 --env prod invoke simple < event.json
23+
destroy:
24+
aws cloudformation delete-stack \
25+
--stack-name $(STACK_NAME)
2726

27+
deploy: build
28+
sam validate --template template.yaml
29+
sam package --template-file template.yaml --s3-bucket $(DEPLOY_S3_BUCKET) --s3-prefix $(DEPLOY_S3_PREFIX) --output-template-file packaged.yaml
30+
sam deploy --template-file ./packaged.yaml --stack-name $(STACK_NAME) --capabilities CAPABILITY_IAM
2831

29-
.PHONY: dev demo prod testdev testdemo testprod
32+
lint:
33+
cfn-lint template.yaml

README.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
[![Build Status](https://travis-ci.org/unee-t/lambda2sns.svg?branch=master)](https://travis-ci.org/unee-t/lambda2sns)
22

3+
<img src="https://media.dev.unee-t.com/2019-08-02/lambda2sns.png" alt="Lambda2sns">
4+
5+
lambda2sns originally started life as a bridge for [Aurora CALL
6+
mysql.lambda_async](https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/AuroraMySQL.Integrating.Lambda.html)
7+
payloads to SNS to be subscribed to. It has evolved to do a lot more, with most
8+
of the complexity coming from the requirement to write results back to the
9+
originating database.
10+
311
lambda: arn:aws:lambda:ap-southeast-1:812644853088:function:alambda_simple
412
sns: arn:aws:sns:ap-southeast-1:812644853088:atest
513

@@ -25,12 +33,6 @@ Then you should get an email of the JSON payload.
2533

2634
# Setup
2735

28-
## lambda role
29-
30-
Assuming project.demo.json is created with correct profile.
31-
32-
apex -r ap-southeast-1 --env demo init
33-
3436
## sns topic
3537

3638
[hendry@t480s alambda]$ aws --profile uneet-demo sns create-topic --name atest

event.json

Lines changed: 0 additions & 5 deletions
This file was deleted.

findError.sh

Lines changed: 0 additions & 31 deletions
This file was deleted.
Lines changed: 49 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,26 @@ func main() {
7272
lambda.Start(handler)
7373
}
7474

75+
type SQSevent struct {
76+
Records []struct {
77+
MessageID string `json:"messageId"`
78+
ReceiptHandle string `json:"receiptHandle"`
79+
Body string `json:"body"`
80+
Attributes struct {
81+
ApproximateReceiveCount string `json:"ApproximateReceiveCount"`
82+
SentTimestamp string `json:"SentTimestamp"`
83+
SenderID string `json:"SenderId"`
84+
ApproximateFirstReceiveTimestamp string `json:"ApproximateFirstReceiveTimestamp"`
85+
} `json:"attributes"`
86+
MessageAttributes struct {
87+
} `json:"messageAttributes"`
88+
Md5OfBody string `json:"md5OfBody"`
89+
EventSource string `json:"eventSource"`
90+
EventSourceARN string `json:"eventSourceARN"`
91+
AwsRegion string `json:"awsRegion"`
92+
} `json:"Records"`
93+
}
94+
7595
func handler(ctx context.Context, evt json.RawMessage) error {
7696

7797
c := withRequestID{}
@@ -84,27 +104,44 @@ func handler(ctx context.Context, evt json.RawMessage) error {
84104
log.Warn("no requestID context")
85105
}
86106

87-
// if err := DB.Ping(); err != nil {
88-
// c.log.WithError(err).Fatal("failed to ping DB")
89-
// }
90-
// c.log.WithField("evt", evt).Info("ping")
91-
107+
var sqsMessage SQSevent
92108
var dat map[string]interface{}
93-
if err := json.Unmarshal(evt, &dat); err != nil {
94-
return err
109+
110+
// Check if SQS event https://github.com/unee-t/lambda2sns/issues/21
111+
err := json.Unmarshal(evt, &sqsMessage)
112+
if err == nil && len(sqsMessage.Records) > 0 {
113+
log.WithField("body", sqsMessage.Records[0].Body).Info("SQS interface")
114+
err = json.Unmarshal([]byte(sqsMessage.Records[0].Body), &dat)
115+
if err != nil {
116+
return err
117+
}
118+
} else {
119+
log.Info("Lambda interface")
120+
err = json.Unmarshal(evt, &dat)
121+
if err != nil {
122+
return err
123+
}
95124
}
96125

126+
log.WithField("dat", dat).Info("Payload")
127+
128+
// What type of payload is this?
97129
_, actionType := dat["actionType"].(string)
130+
// Use dat to replace evt, since it might be parsed out of SQS
131+
evt, err = json.Marshal(dat)
132+
if err != nil {
133+
return err
134+
}
98135

99136
if actionType {
100-
c.log.WithField("payload", evt).Info("actionType")
137+
c.log.WithField("evt", evt).Info("actionType")
101138
err := c.actionTypeDB(evt)
102139
if err != nil {
103-
// c.log.WithError(err).Error("actionTypeDB")
140+
c.log.WithError(err).Error("actionTypeDB")
104141
return err
105142
}
106143
} else {
107-
c.log.WithField("payload", evt).Info("postChangeMessage")
144+
c.log.WithField("evt", evt).Info("postChangeMessage")
108145
err := c.postChangeMessage(evt)
109146
if err != nil {
110147
c.log.WithError(err).Error("postChangeMessage")
@@ -130,6 +167,8 @@ func (c withRequestID) actionTypeDB(evt json.RawMessage) (err error) {
130167

131168
var act actionType
132169

170+
log.WithField("evt", evt).Info("in actiontypeDB")
171+
133172
if err := json.Unmarshal(evt, &act); err != nil {
134173
c.log.WithError(err).Fatal("unable to unmarshall payload")
135174
return err

project.dev.json

Lines changed: 0 additions & 17 deletions
This file was deleted.

sqs/put

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/bin/bash
2+
source ./.env
3+
test -f "$1" || exit
4+
if jq -e . >/dev/null 2>&1 <"$1"; then
5+
aws --profile uneet-dev sqs send-message --queue-url $QUEUE --message-body file://$1
6+
else
7+
echo $1 is not JSON
8+
fi

template.yaml

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
AWSTemplateFormatVersion: '2010-09-09'
2+
Transform: AWS::Serverless-2016-10-31
3+
Description: SAM Lambda2SNS
4+
5+
Globals:
6+
Function:
7+
Runtime: go1.x
8+
Timeout: 60
9+
10+
Resources:
11+
SAMLambda:
12+
Type: AWS::Serverless::Function
13+
Properties:
14+
VpcConfig:
15+
SecurityGroupIds:
16+
- sg-66390301
17+
SubnetIds:
18+
- subnet-0ff046ccc4e3b6281
19+
- subnet-0938728dfb344b970
20+
- subnet-0e123bd457c082cff
21+
Role: !GetAtt LambdaRole.Arn
22+
CodeUri: .
23+
FunctionName: alambda_simple
24+
Tracing: Active
25+
Handler: lambda2sns
26+
Runtime: go1.x
27+
DeadLetterQueue:
28+
Type: SQS
29+
TargetArn: !GetAtt [RetryDeleteQueue, Arn]
30+
31+
RetrySAMLambda:
32+
Type: AWS::Serverless::Function
33+
Properties:
34+
Role: !GetAtt LambdaRole.Arn
35+
CodeUri: .
36+
FunctionName: alambda_simple_retry
37+
VpcConfig:
38+
SecurityGroupIds:
39+
- sg-66390301
40+
SubnetIds:
41+
- subnet-0ff046ccc4e3b6281
42+
- subnet-0938728dfb344b970
43+
- subnet-0e123bd457c082cff
44+
Tracing: Active
45+
Handler: lambda2sns
46+
Runtime: go1.x
47+
Events:
48+
SQSEvent:
49+
Type: SQS
50+
Properties:
51+
Queue: !GetAtt [RetryDeleteQueue, Arn]
52+
BatchSize: 1
53+
54+
RetryDeleteDeadLetterQueue:
55+
Type: AWS::SQS::Queue
56+
Properties:
57+
MessageRetentionPeriod: 1209600
58+
59+
RetryDeleteQueue:
60+
Type: AWS::SQS::Queue
61+
Properties:
62+
VisibilityTimeout: 90
63+
MessageRetentionPeriod: 604800
64+
RedrivePolicy:
65+
deadLetterTargetArn: !GetAtt [RetryDeleteDeadLetterQueue, Arn]
66+
maxReceiveCount: 3
67+
68+
LambdaRole:
69+
Type: AWS::IAM::Role
70+
Properties:
71+
AssumeRolePolicyDocument:
72+
Statement:
73+
- Effect: Allow
74+
Principal:
75+
Service: [lambda.amazonaws.com]
76+
Action: ['sts:AssumeRole']
77+
ManagedPolicyArns:
78+
- arn:aws:iam::aws:policy/AWSLambdaExecute
79+
- arn:aws:iam::aws:policy/AWSXrayWriteOnlyAccess
80+
- arn:aws:iam::aws:policy/AmazonSSMReadOnlyAccess
81+
- arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
82+
- arn:aws:iam::aws:policy/service-role/AWSLambdaRole
83+
- arn:aws:iam::aws:policy/service-role/AWSLambdaVPCAccessExecutionRole
84+
- arn:aws:iam::aws:policy/AmazonSNSFullAccess
85+
- arn:aws:iam::aws:policy/AmazonSQSFullAccess
86+
- arn:aws:iam::aws:policy/service-role/AWSLambdaSQSQueueExecutionRole

0 commit comments

Comments
 (0)