Skip to content
This repository was archived by the owner on Feb 6, 2024. It is now read-only.

Commit bce6c5f

Browse files
committed
feat: merge infra repo
1 parent e9a7366 commit bce6c5f

File tree

13 files changed

+303
-0
lines changed

13 files changed

+303
-0
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,8 @@ lib/
3131
.firebase/
3232
firebase.json
3333
.firebaserc
34+
35+
.terraform
36+
37+
*.tfstate
38+
*.tfstate.backup

infra/.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.terraform
2+
*.hi
3+
*.o
4+
5+
*.tfstate
6+
*.tfstate.backup

infra/api_gateway.tf

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
resource "aws_api_gateway_rest_api" "example" {
2+
name = "ServerlessExample"
3+
description = "Terraform Serverless Application Example"
4+
}
5+
6+
resource "aws_api_gateway_resource" "proxy" {
7+
rest_api_id = "${aws_api_gateway_rest_api.example.id}"
8+
parent_id = "${aws_api_gateway_rest_api.example.root_resource_id}"
9+
path_part = "{proxy+}"
10+
}
11+
12+
resource "aws_api_gateway_method" "proxy" {
13+
rest_api_id = "${aws_api_gateway_rest_api.example.id}"
14+
resource_id = "${aws_api_gateway_resource.proxy.id}"
15+
http_method = "ANY"
16+
authorization = "NONE"
17+
}
18+
19+
resource "aws_api_gateway_integration" "lambda" {
20+
rest_api_id = "${aws_api_gateway_rest_api.example.id}"
21+
resource_id = "${aws_api_gateway_method.proxy.resource_id}"
22+
http_method = "${aws_api_gateway_method.proxy.http_method}"
23+
24+
integration_http_method = "POST"
25+
type = "AWS_PROXY"
26+
uri = "${aws_lambda_function.example.invoke_arn}"
27+
}
28+
29+
resource "aws_api_gateway_method" "proxy_root" {
30+
rest_api_id = "${aws_api_gateway_rest_api.example.id}"
31+
resource_id = "${aws_api_gateway_rest_api.example.root_resource_id}"
32+
http_method = "ANY"
33+
authorization = "NONE"
34+
}
35+
36+
resource "aws_api_gateway_integration" "lambda_root" {
37+
rest_api_id = "${aws_api_gateway_rest_api.example.id}"
38+
resource_id = "${aws_api_gateway_method.proxy_root.resource_id}"
39+
http_method = "${aws_api_gateway_method.proxy_root.http_method}"
40+
41+
integration_http_method = "POST"
42+
type = "AWS_PROXY"
43+
uri = "${aws_lambda_function.example.invoke_arn}"
44+
}
45+
46+
resource "aws_api_gateway_deployment" "example" {
47+
depends_on = [
48+
"aws_api_gateway_integration.lambda",
49+
"aws_api_gateway_integration.lambda_root",
50+
]
51+
52+
rest_api_id = "${aws_api_gateway_rest_api.example.id}"
53+
stage_name = "test"
54+
}
55+
56+
57+
output "base_url" {
58+
value = "${aws_api_gateway_deployment.example.invoke_url}"
59+
}

infra/default.nix

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
with { pkgs = import ./nix {}; };
2+
3+
rec
4+
{ function = pkgs.runCommand "build-function" {}
5+
''
6+
cp ${./main.js} main.js
7+
# Can't be called 'main' otherwise lambda tries to load it
8+
cp ${main}/bin/deckdeckgo-handler main_hs
9+
mkdir $out
10+
${pkgs.zip}/bin/zip -r $out/function.zip main.js main_hs
11+
'';
12+
13+
main = pkgs.haskellPackagesStatic.deckdeckgo-handler;
14+
15+
}

infra/handler/Main.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
main :: IO ()
2+
main = putStrLn "Hello, this is DeckDeckGo!"

infra/handler/package.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
name: deckdeckgo-handler
2+
3+
dependencies:
4+
- base
5+
6+
executable:
7+
main: Main.hs

infra/lambda.tf

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
provider "aws" {
2+
region = "us-east-1"
3+
}
4+
5+
resource "aws_lambda_function" "example" {
6+
function_name = "ServerlessExample"
7+
8+
# The lambda function
9+
filename = "${data.external.build-function.result.build_function_zip_path}"
10+
11+
# "main" is the filename within the zip file (main.js) and "handler"
12+
# is the name of the property under which the handler function was
13+
# exported in that file.
14+
handler = "main.handler"
15+
runtime = "nodejs6.10"
16+
17+
role = "${aws_iam_role.lambda_exec.arn}"
18+
}
19+
20+
# IAM role which dictates what other AWS services the Lambda function
21+
# may access.
22+
resource "aws_iam_role" "lambda_exec" {
23+
name = "serverless_example_lambda"
24+
25+
assume_role_policy = <<EOF
26+
{
27+
"Version": "2012-10-17",
28+
"Statement": [
29+
{
30+
"Action": "sts:AssumeRole",
31+
"Principal": {
32+
"Service": "lambda.amazonaws.com"
33+
},
34+
"Effect": "Allow",
35+
"Sid": ""
36+
}
37+
]
38+
}
39+
EOF
40+
}
41+
42+
data "external" "build-function" {
43+
44+
program = [
45+
"${path.module}/script/build-function"
46+
]
47+
48+
}
49+
50+
resource "aws_lambda_permission" "apigw" {
51+
statement_id = "AllowAPIGatewayInvoke"
52+
action = "lambda:InvokeFunction"
53+
function_name = "${aws_lambda_function.example.arn}"
54+
principal = "apigateway.amazonaws.com"
55+
56+
# The /*/* portion grants access from any method on any resource
57+
# within the API Gateway "REST API".
58+
source_arn = "${aws_api_gateway_deployment.example.execution_arn}/*/*"
59+
}
60+
61+
output "function_zip" {
62+
value = "${data.external.build-function.result.build_function_zip_path}"
63+
}

infra/main.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
'use strict'
2+
3+
exports.handler = function(event, context, callback) {
4+
5+
var fs = require('fs');
6+
var files = fs.readdirSync('.');
7+
8+
var date = new Date();
9+
var current_hour = date.getHours();
10+
var current_minute = date.getMinutes();
11+
12+
var body = "";
13+
14+
try {
15+
var exec = require('child_process').exec;
16+
exec('./main_hs', (error, stdout, stderr) => {
17+
body += `<h1>${stdout}</h1>`;
18+
var response = {
19+
statusCode: 200,
20+
headers: {
21+
'Content-Type': 'text/html; charset=utf-8'
22+
},
23+
body: body
24+
}
25+
callback(null, response)
26+
});
27+
}
28+
catch(error) {
29+
body += "\n<h2>ERROR: "
30+
body += error
31+
body += "</h2>"
32+
var response = {
33+
statusCode: 500,
34+
headers: {
35+
'Content-Type': 'text/html; charset=utf-8'
36+
},
37+
body: body
38+
}
39+
callback(null, response)
40+
}
41+
42+
}

infra/nix/default.nix

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{}:
2+
with rec
3+
{ sources = import ./sources.nix;
4+
pkgs = import sources.nixpkgs {};
5+
6+
pkgsStatic =
7+
(import "${sources.static-haskell-nix}/survey"
8+
{ overlays = [ ] ; normalPkgs = import sources.nixpkgs {}; }).pkgs;
9+
10+
normalHaskellPackages = pkgsStatic.pkgsMusl.haskellPackages.override
11+
{ overrides = self: super: super //
12+
{ deckdeckgo-handler = super.callCabal2nix "handler"
13+
(pkgs.lib.cleanSource ../handler) {};
14+
};
15+
};
16+
17+
haskellPackagesStatic =
18+
(import "${sources.static-haskell-nix}/survey"
19+
{ overlays = [ ] ; normalPkgs = pkgs; inherit normalHaskellPackages; }
20+
).haskellPackages;
21+
};
22+
23+
pkgs //
24+
{ inherit haskellPackagesStatic sources;
25+
inherit (import sources.niv {}) niv;
26+
}

infra/nix/sources.json

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"nixpkgs": {
3+
"url": "https://github.com/NixOS/nixpkgs/archive/88ae8f7d55efa457c95187011eb410d097108445.tar.gz",
4+
"owner": "NixOS",
5+
"branch": "nixos-18.09",
6+
"url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz",
7+
"repo": "nixpkgs",
8+
"sha256": "0qvpzmphdcc80xp88h0ciiia6w25010piwxvslv3b5q6s9m3f6fm",
9+
"description": "Nixpkgs/NixOS branches that track the Nixpkgs/NixOS channels",
10+
"rev": "88ae8f7d55efa457c95187011eb410d097108445"
11+
},
12+
"static-haskell-nix": {
13+
"homepage": "",
14+
"url": "https://github.com/nh2/static-haskell-nix/archive/9781df8a48eade302d159ce63a7ab0c30247788c.tar.gz",
15+
"owner": "nh2",
16+
"branch": "master",
17+
"url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz",
18+
"repo": "static-haskell-nix",
19+
"sha256": "0bmjcza91srz29xxwizzkzzliypb9f8ry84s636x680d168i39hh",
20+
"description": "easily build most Haskell programs into fully static Linux executables",
21+
"rev": "9781df8a48eade302d159ce63a7ab0c30247788c"
22+
},
23+
"niv": {
24+
"homepage": "https://github.com/nmattia/niv",
25+
"url": "https://github.com/nmattia/niv/archive/7f72d723d00fcc3f177138acbfcf9d581beee9e1.tar.gz",
26+
"owner": "nmattia",
27+
"branch": "master",
28+
"url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz",
29+
"repo": "niv",
30+
"sha256": "0177xhz55519xrak1fmv3gilbg8330lmbfkbizkc8zgp58skcmjw",
31+
"description": "Easy dependency management for Nix projects",
32+
"rev": "7f72d723d00fcc3f177138acbfcf9d581beee9e1"
33+
}
34+
}

0 commit comments

Comments
 (0)