Skip to content

Commit 70c22ef

Browse files
committed
spectral-terraform-lambda-integration
0 parents  commit 70c22ef

21 files changed

+755
-0
lines changed

.github/workflows/scan.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
name: Main
2+
on:
3+
push:
4+
branches: [ main ]
5+
pull_request:
6+
7+
env:
8+
SPECTRAL_DSN: ${{ secrets.SPECTRAL_DSN }}
9+
10+
jobs:
11+
scan:
12+
runs-on: ubuntu-latest
13+
steps:
14+
- uses: actions/checkout@v2
15+
- name: Install and run Spectral CI
16+
uses: spectralops/spectral-github-action@v2
17+
with:
18+
spectral-dsn: ${{ env.SPECTRAL_DSN }}
19+
spectral-args: scan --ok --include-tags base,iac

.github/workflows/terraform-fmt.yml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
name: "terraform formatting"
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
pull_request:
8+
9+
defaults:
10+
run:
11+
shell: bash
12+
13+
jobs:
14+
terraform:
15+
name: "Terraform"
16+
runs-on: ubuntu-latest
17+
steps:
18+
- name: Checkout
19+
uses: actions/checkout@v2
20+
- name: Setup Terraform
21+
uses: hashicorp/setup-terraform@v1
22+
- name: Terraform format - root
23+
id: fmt_root
24+
run: terraform fmt -check
25+
- name: Terraform format - lambda module
26+
id: fmt_lambda
27+
run: terraform fmt -check ./modules/lambda/
28+
- name: Terraform format - lambda module
29+
id: fmt_api_gateway
30+
run: terraform fmt -check ./modules/api_gateway/

.gitignore

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
**/.terraform/*
2+
*.tfstate
3+
*.tfstate.*
4+
crash.log
5+
crash.*.log
6+
*.tfvars
7+
*.tfvars.json
8+
override.tf
9+
override.tf.json
10+
*_override.tf
11+
*_override.tf.json
12+
.terraformrc
13+
terraform.rc

README.md

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
<img src="https://user-images.githubusercontent.com/44297242/188002580-ba0a6d59-8b1c-475e-bd61-192dd952194f.png" alt="drawing" style="width:400px;"/>
2+
3+
# spectral-lambda-integration
4+
5+
Terraform configuration used to create the required AWS resources for integrating between Spectral and external service providers.
6+
7+
## Requirements
8+
9+
| Name | Version |
10+
| ----------- | ----------- |
11+
| [terraform](https://www.terraform.io/downloads) | ~> 1.2.0 |
12+
13+
## Providers
14+
15+
| Name | Version |
16+
| ----------- | ----------- |
17+
| [aws](https://registry.terraform.io/providers/hashicorp/aws/latest/docs) | ~> 4.9 |
18+
19+
## Inputs
20+
21+
| Name | Description | Type | Default | Required |
22+
| ----------- | ----------- | ----------- | ----------- | ----------- |
23+
| `account_id` | AWS Account ID number of the account in which to manage resources. | `number` | N/A | Yes |
24+
| `aws_region` | The region in which to manage resources.| `string` | N/A | Yes |
25+
| `environment` | The target environment name for deployment | `string` | `prod` | No |
26+
| `integration_type` | Spectral integration type (A unique phrase describing the integration) - Available values: `terraform` | `string` | N/A | Yes |
27+
| [`env_vars`](#env_vars) | Extendable object contains all required environment variables required for the integration. | `map(string)` | N/A | No |
28+
| [`global_tags`](#global_tags) | Tags to be applied on every newly created resource. | `map(string)` | ```{ BusinessUnit = "Spectral" }``` | No |
29+
| [`tags`](#tags) | Tags to be applied on concrete resources | `map(map(string))` | ```{ iam = { } lambda = { } api_gateway = { } }``` | No |
30+
| `lambda_enable_logs` | Specifies if Lambda should have CloudWatch a dedicated logs group. | `bool` | `false` | No |
31+
| `lambda_logs_retention_in_days` | Specifies the number of days you want to retain log events in the specified log group. | `number` | `30` | No |
32+
| `lambda_function_timeout` | Amount of time your Lambda Function has to run in seconds. | `number` | 300 | No |
33+
| `lambda_function_memory_size` | Amount of memory in MB your Lambda Function can use at runtime. | `number` | 1024 | No |
34+
| `lambda_publish` | Whether to publish creation/change as new Lambda Function Version. | `bool` | `false` | No |
35+
36+
### env_vars
37+
38+
In some integrations, Spectral requires some extra environment variables besides the default ones.
39+
Those extra variables should be added to the `env_vars` map in addition to `CHECK_POLICY` and `SPECTRAL_DSN` which are mandatory.
40+
41+
Please refer to our docs to view the extra environment variables needed for the integration.
42+
43+
##### SPECTRAL_DSN (mandatory)
44+
45+
Your SpectralOps identifier, retrieved from your SpectralOps account.
46+
47+
##### CHECK_POLICY (mandatory)
48+
49+
`CHECK_POLICY` responsible for setting the minimum issue severity that should fail the check.
50+
The valid values for this field are:
51+
52+
1. Fail on any issue
53+
2. Fail on warnings and above
54+
3. Fail on errors only
55+
4. Always pass
56+
57+
### global_tags
58+
59+
This variable holds a list of tags be applied on all newly created resources:
60+
61+
```tcl
62+
{
63+
BusinessUnit = "Spectral"
64+
...
65+
}
66+
```
67+
68+
### tags
69+
70+
This variable holds a collection of tags grouped by key representing its target resource:
71+
72+
1. IAM role resource - using the `iam` key
73+
2. Lambda resource - using the `lambda` key
74+
3. ApiGateway resource - using the `api_gateway` key
75+
76+
```tcl
77+
{
78+
iam = {
79+
...
80+
}
81+
lambda = {
82+
...
83+
}
84+
api_gateway = {
85+
...
86+
}
87+
}
88+
```
89+
90+
## Usage
91+
92+
```tcl
93+
module "spectral_lambda_integration" {
94+
source = "github.com/SpectralOps/spectral-terraform-lambda-integration?ref=v1.0.0"
95+
96+
account_id = 111111111111
97+
aws_region = "us-east-1"
98+
environment = "prod"
99+
integration_type = "terraform"
100+
lambda_enable_logs = true
101+
lambda_logs_retention_in_days = 30
102+
lambda_publish = false
103+
lambda_function_timeout = 300
104+
lambda_function_memory_size = 1024
105+
106+
# Environment variables used by the integration
107+
env_vars = {
108+
# Mandatory - Your spectral DSN retreived from SpectralOps
109+
SPECTRAL_DSN = ""
110+
# Mandatory - Set which severity should fail the check
111+
CHECK_POLICY = ""
112+
# Additional env-vars should go here
113+
}
114+
115+
# Global tags - Tags to be applied on every newly created resource
116+
global_tags = {
117+
# Tags to apply to all newly created resources
118+
BusinessUnit = "Spectral"
119+
}
120+
121+
# Tags to be applied on concrete resources
122+
tags = {
123+
# Tags to apply on iam related resources
124+
iam = {
125+
Resource = "role"
126+
}
127+
# Tags to apply on lambda related resources
128+
lambda = {
129+
Resource = "lambda"
130+
}
131+
# Tags to apply on api_gateway related resources
132+
api_gateway = {
133+
Resource = "api_gateway"
134+
}
135+
}
136+
}
137+
```
138+
139+
## Resources
140+
141+
| Name | Type |
142+
| ----------- | ----------- |
143+
| [aws_api_gateway_rest_api](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/api_gateway_rest_api) | resource |
144+
| [aws_api_gateway_method](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/api_gateway_method) | resource |
145+
| [aws_api_gateway_method_response](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/api_gateway_method_response) | resource |
146+
| [aws_api_gateway_integration](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/api_gateway_integration) | resource |
147+
| [aws_api_gateway_deployment](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/api_gateway_deployment) | resource |
148+
| [aws_api_gateway_stage](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/api_gateway_stage) | resource |
149+
| [aws_lambda_permission](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lambda_permission) | resource |
150+
| [aws_lambda_function](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lambda_function) | resource |
151+
| [aws_cloudwatch_log_group](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_log_group) | resource |
152+
| [aws_iam_role](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role) | resource |
153+
| [aws_iam_role_policy_attachment](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
154+
| [aws_iam_policy_document](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data |
155+
156+
## Outputs
157+
158+
### This module has the following outputs
159+
160+
1. `rest_api_id` - The ID of the REST API.
161+
2. `rest_api_url` - The URL for accessing the lambda through the ApiGateway.
162+
3. `rest_api_arn` - Amazon Resource Name (ARN) identifying your Rest API.
163+
4. `rest_api_execution_arn` - The execution ARN part to be used in lambda_permission's source_arn, not concatenated to other allowed API resources.
164+
5. `rest_api_lambda_execution_arn` - The execution ARN part to be used in lambda_permission's source_arn, concatenated with allowed API resources (method & path).
165+
6. `lambda_function_arn` - Amazon Resource Name (ARN) identifying your Lambda Function.
166+
7. `lambda_function_name` - The name of the lambda function.
167+
8. `lambda_iam_role_arn` - Amazon Resource Name (ARN) specifying the role.
168+
9. `lambda_iam_role_name` - Name of the role.

examples/basic-integration.tf

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module "spectral_lambda_integration" {
2+
source = "github.com/SpectralOps/spectral-terraform-lambda-integration?ref=v1.0.0"
3+
4+
account_id = 111111111111
5+
aws_region = "us-east-1"
6+
integration_type = "terraform"
7+
8+
env_vars = {
9+
SPECTRAL_DSN = "MySpectralDSN"
10+
CHECK_POLICY = "Always Pass"
11+
}
12+
}

examples/custom-lambda-settings.tf

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
module "spectral_lambda_integration" {
2+
source = "github.com/SpectralOps/spectral-terraform-lambda-integration?ref=v1.0.0"
3+
4+
account_id = 111111111111
5+
aws_region = "us-east-1"
6+
integration_type = "terraform"
7+
8+
lambda_function_timeout = 320
9+
lambda_function_memory_size = 1024
10+
lambda_publsh = true
11+
12+
env_vars = {
13+
SPECTRAL_DSN = "MySpectralDSN"
14+
CHECK_POLICY = "Always Pass"
15+
}
16+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module "spectral_lambda_integration" {
2+
source = "github.com/SpectralOps/spectral-terraform-lambda-integration?ref=v1.0.0"
3+
4+
account_id = 111111111111
5+
aws_region = "us-east-1"
6+
integration_type = "terraform"
7+
lambda_enable_logs = true
8+
lambda_logs_retention_in_days = 10
9+
10+
env_vars = {
11+
SPECTRAL_DSN = "MySpectralDSN"
12+
CHECK_POLICY = "Always Pass"
13+
}
14+
}

examples/extended-tags.tf

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
module "spectral_lambda_integration" {
2+
source = "github.com/SpectralOps/spectral-terraform-lambda-integration?ref=v1.0.0"
3+
4+
account_id = 111111111111
5+
aws_region = "us-east-1"
6+
integration_type = "terraform"
7+
8+
tags = {
9+
iam = {
10+
Component = "IAM"
11+
}
12+
lambda = {
13+
Component = "Lambda"
14+
}
15+
api_gateway = {
16+
Component = "ApiGateway"
17+
}
18+
}
19+
20+
global_tags = {
21+
BusinessUnit = "Spectral"
22+
SomeGlobalTag = "Value"
23+
}
24+
25+
env_vars = {
26+
SPECTRAL_DSN = "MySpectralDSN"
27+
CHECK_POLICY = "Always Pass"
28+
}
29+
}

examples/extra-env-vars.tf

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module "spectral_lambda_integration" {
2+
source = "github.com/SpectralOps/spectral-terraform-lambda-integration?ref=v1.0.0"
3+
4+
account_id = 111111111111
5+
aws_region = "us-east-1"
6+
integration_type = "terraform"
7+
8+
env_vars = {
9+
SPECTRAL_DSN = "MySpectralDSN"
10+
CHECK_POLICY = "Always Pass"
11+
TERRFORM_USER_KEY = "MY-KEY"
12+
GITHUB_TOKEN = "MY-TOKEN"
13+
# Extra environment variables goes here...
14+
}
15+
}

main.tf

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
locals {
2+
resource_name_pattern = "spectral-${var.integration_type}-integration-${var.environment}"
3+
}
4+
5+
module "lambda_function" {
6+
source = "./modules/lambda"
7+
global_tags = var.global_tags
8+
tags = var.tags
9+
environment = var.environment
10+
integration_type = var.integration_type
11+
resource_name_pattern = local.resource_name_pattern
12+
env_vars = var.env_vars
13+
logs_retention_in_days = var.lambda_logs_retention_in_days
14+
should_write_logs = var.lambda_enable_logs
15+
timeout = var.lambda_function_timeout
16+
memory_size = var.lambda_function_memory_size
17+
publish = var.lambda_publish
18+
}
19+
20+
module "api_gateway" {
21+
source = "./modules/api_gateway"
22+
global_tags = var.global_tags
23+
tags = var.tags
24+
environment = var.environment
25+
integration_type = var.integration_type
26+
resource_name_pattern = local.resource_name_pattern
27+
lambda_function_arn = module.lambda_function.lambda_function_arn
28+
}

modules/api_gateway/outputs.tf

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
output "rest_api_url" {
2+
value = "${aws_api_gateway_deployment.rest_api_deployment.invoke_url}${aws_api_gateway_stage.rest_api_stage.stage_name}${aws_api_gateway_resource.event_resource.path}"
3+
}
4+
5+
output "rest_api_arn" {
6+
value = aws_api_gateway_rest_api.gateway_rest_api.arn
7+
}
8+
9+
output "rest_api_execution_arn" {
10+
value = aws_api_gateway_rest_api.gateway_rest_api.execution_arn
11+
}
12+
13+
output "rest_api_lambda_execution_arn" {
14+
value = local.rest_api_execution_arn
15+
}
16+
17+
output "rest_api_id" {
18+
value = aws_api_gateway_rest_api.gateway_rest_api.id
19+
}

0 commit comments

Comments
 (0)