-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
64f0e80
commit 165ff89
Showing
7 changed files
with
203 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
.terraform | ||
.terraform.lock.hcl | ||
terraform.tfstate* | ||
Makefile |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
FROM public.ecr.aws/lambda/provided:al2 as build | ||
# install compiler | ||
RUN yum install -y golang | ||
RUN go env -w GOPROXY=direct | ||
# cache dependencies | ||
ADD go.mod go.sum ./ | ||
RUN go mod download | ||
# build | ||
ADD . . | ||
RUN go build -o /main | ||
# copy artifacts to a clean image | ||
FROM public.ecr.aws/lambda/provided:al2 | ||
COPY --from=build /main /main | ||
ENTRYPOINT [ "/main" ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# Build image | ||
|
||
1. Build & Push image | ||
|
||
``` | ||
docker build -t 270431303334.dkr.ecr.ap-southeast-2.amazonaws.com/hello-world:latest .; | ||
aws ecr get-login-password --region ap-southeast-2 | docker login --username AWS --password-stdin 270431303334.dkr.ecr.ap-southeast-2.amazonaws.com; | ||
docker push 270431303334.dkr.ecr.ap-southeast-2.amazonaws.com/hello-world:latest; | ||
``` | ||
|
||
2. Run terraform | ||
|
||
``` | ||
terraform init; | ||
terraform apply; | ||
``` | ||
|
||
3. Test | ||
|
||
``` | ||
curl --location --request POST 'https://ssah8op3a5.execute-api.ap-southeast-2.amazonaws.com/default/FakeChimera' --header 'Content-Type: application/json' --data-raw '{}' | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
module github.com/ledongthuc/lambdaslackbot | ||
|
||
go 1.16 | ||
|
||
require ( | ||
github.com/aws/aws-lambda-go v1.23.0 | ||
github.com/sirupsen/logrus v1.8.1 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= | ||
github.com/aws/aws-lambda-go v1.23.0 h1:Vjwow5COkFJp7GePkk9kjAo/DyX36b7wVPKwseQZbRo= | ||
github.com/aws/aws-lambda-go v1.23.0/go.mod h1:jJmlefzPfGnckuHdXX7/80O3BvUUi12XOkbv4w9SGLU= | ||
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU= | ||
github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU= | ||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= | ||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | ||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= | ||
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= | ||
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE= | ||
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= | ||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= | ||
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= | ||
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0= | ||
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= | ||
github.com/urfave/cli/v2 v2.2.0/go.mod h1:SE9GqnLQmjVa0iPEY0f1w3ygNIYcIJ0OKPMoW2caLfQ= | ||
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4= | ||
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= | ||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= | ||
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776 h1:tQIYjPdBoyREyB9XMu+nnTclpTYkz2zFM+lzLJFO4gQ= | ||
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/base64" | ||
"net/url" | ||
"os" | ||
|
||
"github.com/aws/aws-lambda-go/events" | ||
"github.com/aws/aws-lambda-go/lambda" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
func HandleRequest(request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) { | ||
logrus.SetFormatter(&logrus.JSONFormatter{}) | ||
logrus.WithFields(logrus.Fields{ | ||
"env": os.Environ(), | ||
"request_id": request.RequestContext.RequestID, | ||
"method": request.HTTPMethod, | ||
"path": request.Path, | ||
"res": request.Resource, | ||
"body": request.Body, | ||
"is_base64_encoded": request.IsBase64Encoded, | ||
}).Info("New request") | ||
|
||
body := request.Body | ||
if request.IsBase64Encoded { | ||
b, err := base64.URLEncoding.DecodeString(request.Body) | ||
if err != nil { | ||
logrus.Error(err) | ||
return events.APIGatewayProxyResponse{ | ||
Body: "Fail to process: decode request: " + err.Error(), | ||
StatusCode: 200, | ||
}, nil | ||
} | ||
body = string(b) | ||
} | ||
|
||
arguments, err := url.ParseQuery(body) | ||
if err != nil { | ||
return events.APIGatewayProxyResponse{ | ||
Body: "Fail to process: parse request: " + err.Error(), | ||
StatusCode: 200, | ||
}, nil | ||
} | ||
logrus.WithFields(logrus.Fields{ | ||
"arguments": arguments, | ||
}).Info("Arguments") | ||
|
||
msg := awsInsatncesMsg() | ||
return events.APIGatewayProxyResponse{ | ||
Body: msg, | ||
StatusCode: 200, | ||
}, nil | ||
} | ||
|
||
func main() { | ||
lambda.Start(HandleRequest) | ||
} | ||
func awsInsatncesMsg() string { | ||
return "Hello guy\nNice to meet you<br/>Good bye" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
provider "aws" { | ||
region = "ap-southeast-2" | ||
} | ||
|
||
resource "aws_iam_role" "iam_for_lambda" { | ||
name = "iam_for_lambda" | ||
|
||
assume_role_policy = <<EOF | ||
{ | ||
"Version": "2012-10-17", | ||
"Statement": [ | ||
{ | ||
"Action": "sts:AssumeRole", | ||
"Principal": { | ||
"Service": "lambda.amazonaws.com" | ||
}, | ||
"Effect": "Allow", | ||
"Sid": "" | ||
} | ||
] | ||
} | ||
EOF | ||
} | ||
|
||
data "aws_iam_policy" "AWSLambdaBasicExecutionRole" { | ||
arn = "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" | ||
} | ||
|
||
resource "aws_iam_role_policy_attachment" "iam_for_lambda_cloudwatch" { | ||
role = aws_iam_role.iam_for_lambda.name | ||
policy_arn = data.aws_iam_policy.AWSLambdaBasicExecutionRole.arn | ||
} | ||
|
||
locals { | ||
path = "/dbbackup" | ||
function_name = "FakeChimera2" | ||
} | ||
|
||
resource "aws_lambda_function" "test_lambda" { | ||
function_name = local.function_name | ||
role = aws_iam_role.iam_for_lambda.arn | ||
|
||
package_type = "Image" | ||
image_uri = "270431303334.dkr.ecr.ap-southeast-2.amazonaws.com/hello-world:latest" | ||
} | ||
|
||
resource "aws_cloudwatch_log_group" "example" { | ||
name = "/aws/lambda/${local.function_name}" | ||
retention_in_days = 7 | ||
} | ||
|
||
resource "aws_apigatewayv2_api" "api" { | ||
name = "api-fake-chimera" | ||
protocol_type = "HTTP" | ||
target = aws_lambda_function.test_lambda.arn | ||
route_key = "POST ${local.path}" | ||
} | ||
|
||
resource "aws_lambda_permission" "apigw" { | ||
action = "lambda:InvokeFunction" | ||
function_name = aws_lambda_function.test_lambda.arn | ||
principal = "apigateway.amazonaws.com" | ||
source_arn = "${aws_apigatewayv2_api.api.execution_arn}/*/*" | ||
} | ||
|
||
output "base_url" { | ||
value = "${aws_apigatewayv2_api.api.api_endpoint}${local.path}" | ||
} |