|
1 |
| -# Terraform AWS Lambda |
| 1 | +# Eventbridge to API call |
2 | 2 |
|
3 |
| -_Template repository for creating a TypeScript AWS lambda function with Terraform_ |
| 3 | +_An AWS Lambda for sending EventBridge events to API endpoint |
4 | 4 |
|
5 |
| -  |
| 5 | +  |
6 | 6 |
|
7 |
| -## How to use with Terraform |
| 7 | +## Description |
8 | 8 |
|
9 |
| -Add the module to your [Terraform](https://www.terraform.io/) project: |
| 9 | +The goal of this module is to send a POST to a specific API endpoint when an EventBridge [rule](https://docs.aws.amazon.com/eventbridge/latest/userguide/create-eventbridge-rule.html) is triggered. |
10 | 10 |
|
11 |
| -```terraform |
12 |
| -module "terraform_aws_lambda" { |
13 |
| - source = "[email protected]:agendrix/terraform-aws-lambda.git//terraform?ref=v0.2.0" |
14 |
| - lambda_name = "my-typescript-lambda" |
15 |
| - role_arn = aws_iam_role.iam_for_lambda.role_arn |
16 |
| -} |
| 11 | +## Lambda payload |
| 12 | + |
| 13 | +The lambda function payload is an object with the following type: |
| 14 | +```ts |
| 15 | +type Payload = { |
| 16 | + headers: { |
| 17 | + "Authorization": string, |
| 18 | + [key: string]: string |
| 19 | + }; |
| 20 | + data: JSON; |
| 21 | +}; |
17 | 22 | ```
|
| 23 | +`headers`: Need at least the key `Authorization` to access the API |
| 24 | + |
| 25 | +`data`: All the data you want to send to the API. Can be empty |
| 26 | + |
| 27 | +## How to use with Terraform |
| 28 | +Add the module to your [Terraform](https://www.terraform.io/) project: |
| 29 | + |
| 30 | +```HCL |
| 31 | +module "eventbridge_logger_opgenie" { |
| 32 | + source = "github.com/agendrix/eventbridge-to-make-api-call.git//terraform?ref=v0.1.2" |
| 33 | + sns_topic_to_notify_on_failure = <aws_sns_topic_arn> |
| 34 | + name = "Ressource Name" |
18 | 35 |
|
19 |
| -See [Resource: aws_lambda_function](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lambda_function) for more information about the required `aws_iam_role`. |
| 36 | + api_config = { |
| 37 | + api_url = "<MY_API_URL>" |
| 38 | + api_key = "<MY_API_KEY_SECRET>" |
| 39 | + } |
20 | 40 |
|
21 |
| -In order to be able to receive http requests to the lambda, you will need to hook it up with an AWS API Gateway. |
22 |
| -You can do so by following this guide: [Serverless Applications with AWS Lambda and API Gateway](https://learn.hashicorp.com/tutorials/terraform/lambda-api-gateway). |
| 41 | + event_pattern = jsonencode({ |
| 42 | + source = ["aws.cloudwatch"] |
| 43 | + detail-type = ["CloudWatch Alarm State Change"] |
| 44 | + }) |
23 | 45 |
|
24 |
| -After applying the terraform plan, a dummy lambda will be available in the [AWS Lambda Console](https://console.aws.amazon.com/lambda/). |
| 46 | + input_transformer = { |
| 47 | + input_paths = { |
| 48 | + source = "$.source" |
| 49 | + time = "$.time" |
| 50 | + alarm = "$.detail.alarmName" |
| 51 | + } |
| 52 | +
|
| 53 | + input_template = <<EOF |
| 54 | + { |
| 55 | + "headers": { |
| 56 | + "Content-Type": "application/json", |
| 57 | + "Authorization": "Key " |
| 58 | + }, |
| 59 | + "data": { |
| 60 | + "message": "My Message <source>", |
| 61 | + "description": "My Description: <time> <alarm>", |
| 62 | + } |
| 63 | + } |
| 64 | + EOF |
| 65 | + } |
| 66 | +} |
| 67 | +``` |
25 | 68 |
|
26 |
| -## Deploying a new version of the lambda |
| 69 | +### Resources: |
27 | 70 |
|
28 |
| -- Make sure you have all the required [GitHub Actions secrets](https://docs.github.com/en/free-pro-team@latest/actions/reference/encrypted-secrets) for [`.github/workflows/release.yml`](.github/workflows/release.yml) to work. |
29 |
| -- Follow [CONTRIBUTING.md / Publish a new release](./CONTRIBUTING.md#publish-a-new-release) for deploying a new release. |
| 71 | +[How to create a rule pattern](https://docs.aws.amazon.com/eventbridge/latest/userguide/eventbridge-and-event-patterns.html) |
30 | 72 |
|
31 |
| ---- |
| 73 | +[Input transformer](https://docs.aws.amazon.com/eventbridge/latest/userguide/transform-input.html) |
32 | 74 |
|
33 |
| -Your AWS lambda should now be available at https://console.aws.amazon.com/lambda/. |
| 75 | +[Input transformer Terraform](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_event_target#input_transformer) |
34 | 76 |
|
35 |
| -Logs from the lambda will be available in AWS CloudWatch `/aws/lambda/${yourLambdaName}` log group. |
| 77 | +[Common input transformer issues](https://docs.aws.amazon.com/eventbridge/latest/userguide/transform-input.html#transform-input-issues) |
0 commit comments