|
| 1 | +# Copyright (c) 2017 Martin Donath <martin.donath@squidfunk.com> |
| 2 | + |
| 3 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 | +# of this software and associated documentation files (the "Software"), to |
| 5 | +# deal in the Software without restriction, including without limitation the |
| 6 | +# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 7 | +# sell copies of the Software, and to permit persons to whom the Software is |
| 8 | +# furnished to do so, subject to the following conditions: |
| 9 | + |
| 10 | +# The above copyright notice and this permission notice shall be included in |
| 11 | +# all copies or substantial portions of the Software. |
| 12 | + |
| 13 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | +# FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE |
| 16 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 18 | +# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 19 | +# IN THE SOFTWARE. |
| 20 | + |
| 21 | +# ----------------------------------------------------------------------------- |
| 22 | +# Data: IAM |
| 23 | +# ----------------------------------------------------------------------------- |
| 24 | + |
| 25 | +# data.template_file.lambda_iam_policy.rendered |
| 26 | +data "template_file" "lambda_iam_policy" { |
| 27 | + template = "${file("${path.root}/aws-iam/policies/lambda.json")}" |
| 28 | + |
| 29 | + vars { |
| 30 | + bucket = "${var.bucket}" |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +# ----------------------------------------------------------------------------- |
| 35 | +# Resources: IAM |
| 36 | +# ----------------------------------------------------------------------------- |
| 37 | + |
| 38 | +# aws_iam_role.lambda |
| 39 | +resource "aws_iam_role" "lambda" { |
| 40 | + name = "${var.namespace}-lambda-cloudwatch" |
| 41 | + path = "/${var.namespace}/lambda/" |
| 42 | + |
| 43 | + assume_role_policy = "${ |
| 44 | + file("${path.root}/aws-iam/policies/assume-role/lambda.json") |
| 45 | + }" |
| 46 | +} |
| 47 | + |
| 48 | +# aws_iam_policy.lambda |
| 49 | +resource "aws_iam_policy" "lambda" { |
| 50 | + name = "${var.namespace}-lambda-cloudwatch" |
| 51 | + path = "/${var.namespace}/lambda/" |
| 52 | + |
| 53 | + policy = "${data.template_file.lambda_iam_policy.rendered}" |
| 54 | +} |
| 55 | + |
| 56 | +# aws_iam_policy_attachment.lambda |
| 57 | +resource "aws_iam_policy_attachment" "lambda" { |
| 58 | + name = "${var.namespace}-lambda-cloudwatch" |
| 59 | + |
| 60 | + policy_arn = "${aws_iam_policy.lambda.arn}" |
| 61 | + roles = ["${aws_iam_role.lambda.id}"] |
| 62 | +} |
| 63 | + |
| 64 | +# ----------------------------------------------------------------------------- |
| 65 | +# Resources: CloudWatch |
| 66 | +# ----------------------------------------------------------------------------- |
| 67 | + |
| 68 | +# aws_cloudwatch_event_rule.status |
| 69 | +resource "aws_cloudwatch_event_rule" "status" { |
| 70 | + name = "${var.namespace}-webhook-status" |
| 71 | + |
| 72 | + event_pattern = "${ |
| 73 | + file("${path.root}/aws-cloudwatch/rules/codebuild.json") |
| 74 | + }" |
| 75 | +} |
| 76 | + |
| 77 | +# aws_cloudwatch_event_target.status |
| 78 | +resource "aws_cloudwatch_event_target" "status" { |
| 79 | + rule = "${aws_cloudwatch_event_rule.status.name}" |
| 80 | + arn = "${aws_lambda_function.status.arn}" |
| 81 | +} |
| 82 | + |
| 83 | +# ----------------------------------------------------------------------------- |
| 84 | +# Resources: Lambda |
| 85 | +# ----------------------------------------------------------------------------- |
| 86 | + |
| 87 | +# aws_lambda_function.status |
| 88 | +resource "aws_lambda_function" "status" { |
| 89 | + function_name = "${var.namespace}-webhook-status" |
| 90 | + role = "${aws_iam_role.lambda.arn}" |
| 91 | + runtime = "nodejs6.10" |
| 92 | + filename = "${path.root}/aws-lambda/dist/status.zip" |
| 93 | + handler = "index.default" |
| 94 | + timeout = 10 |
| 95 | + |
| 96 | + source_code_hash = "${ |
| 97 | + base64sha256(file("${path.root}/aws-lambda/dist/status.zip")) |
| 98 | + }" |
| 99 | + |
| 100 | + environment { |
| 101 | + variables = { |
| 102 | + GITHUB_OAUTH_TOKEN = "${var.github_oauth_token}" |
| 103 | + GITHUB_REPORTER = "${var.github_reporter}" |
| 104 | + CODEBUILD_BUCKET = "${var.bucket}" |
| 105 | + } |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +# aws_lambda_permission.status |
| 110 | +resource "aws_lambda_permission" "status" { |
| 111 | + statement_id = "AllowExecutionFromCloudWatch" |
| 112 | + action = "lambda:InvokeFunction" |
| 113 | + function_name = "${aws_lambda_function.status.arn}" |
| 114 | + principal = "events.amazonaws.com" |
| 115 | + source_arn = "${aws_cloudwatch_event_rule.status.arn}" |
| 116 | +} |
0 commit comments