An open API service indexing awesome lists of open source software.

https://github.com/jameswoolfenden/terraform-aws-lambda

Basic lambda module
https://github.com/jameswoolfenden/terraform-aws-lambda

aws module terraform

Last synced: 2 months ago
JSON representation

Basic lambda module

Awesome Lists containing this project

README

          

# terraform-aws-lambda

[![Build Status](https://github.com/JamesWoolfenden/terraform-aws-lambda/workflows/Verify%20and%20Bump/badge.svg?branch=master)](https://github.com/JamesWoolfenden/terraform-aws-lambda)
[![Latest Release](https://img.shields.io/github/release/JamesWoolfenden/terraform-aws-lambda.svg)](https://github.com/JamesWoolfenden/terraform-aws-lambda/releases/latest)
[![GitHub tag (latest SemVer)](https://img.shields.io/github/tag/JamesWoolfenden/terraform-aws-lambda.svg?label=latest)](https://github.com/JamesWoolfenden/terraform-aws-lambda/releases/latest)
![Terraform Version](https://img.shields.io/badge/tf-%3E%3D0.14.0-blue.svg)
[![Infrastructure Tests](https://www.bridgecrew.cloud/badges/github/JamesWoolfenden/terraform-aws-lambda/cis_aws)](https://www.bridgecrew.cloud/link/badge?vcs=github&fullRepo=JamesWoolfenden%2Fterraform-aws-lambda&benchmark=CIS+AWS+V1.2)
[![pre-commit](https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit&logoColor=white)](https://github.com/pre-commit/pre-commit)
[![checkov](https://img.shields.io/badge/checkov-verified-brightgreen)](https://www.checkov.io/)
[![Infrastructure Tests](https://www.bridgecrew.cloud/badges/github/jameswoolfenden/terraform-aws-lambda/general)](https://www.bridgecrew.cloud/link/badge?vcs=github&fullRepo=JamesWoolfenden%2Fterraform-aws-lambda&benchmark=INFRASTRUCTURE+SECURITY)

---

Minimal Terraform module.

---

It's 100% Open Source and licensed under the [APACHE2](LICENSE).

## Usage

This is a minimal example **examplea**, but with Cloudwatch alarms enabled.

```hcl
module "lambda" {
source = "github.com/jameswoolfenden/terraform-aws-lambda"
version= "0.3.40"
account_id = data.aws_caller_identity.current.account_id
alarms_enabled = true
common_tags = var.common_tags
description = "Best Pizza!!"
filename = "${path.module}/lambda.zip"
name = var.name
region_name = data.aws_region.current.name
role_arn = data.aws_iam_role.lambda.arn
}

```

## Requirements

No requirements.

## Providers

| Name | Version |
|------|---------|
| [aws](#provider\_aws) | n/a |

## Modules

No modules.

## Resources

| Name | Type |
|------|------|
| [aws_iam_policy.basic](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy) | resource |
| [aws_iam_policy.kms](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy) | resource |
| [aws_iam_policy.s3](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy) | resource |
| [aws_iam_policy.vpc](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy) | resource |
| [aws_iam_role.updatepolicy](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role) | resource |
| [aws_iam_role_policy_attachment.basic-attach](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
| [aws_iam_role_policy_attachment.kms-attach](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
| [aws_iam_role_policy_attachment.s3-attach](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
| [aws_iam_role_policy_attachment.vpc-attach](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
| [aws_lambda_function.withintent](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lambda_function) | resource |
| [aws_caller_identity.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/caller_identity) | data source |
| [aws_region.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/region) | data source |

## Inputs

| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| [action](#input\_action) | Action for the Lambda permission | `string` | `"lambda:InvokeFunction"` | no |
| [alarms\_enabled](#input\_alarms\_enabled) | Cloudwatch alarms enabled | `bool` | `false` | no |
| [description](#input\_description) | Of the the Lambda | `string` | n/a | yes |
| [envvar](#input\_envvar) | Optional set of environmental variables for the lambda | `map(any)` | `{}` | no |
| [ephemeral\_storage](#input\_ephemeral\_storage) | Storage for lambda Min 512 MB and the Max 10240 MB | `number` | `512` | no |
| [filename](#input\_filename) | name of zip file if any | `string` | `null` | no |
| [handler](#input\_handler) | The file the lambda should import | `string` | `"index.handler"` | no |
| [kms\_key\_id](#input\_kms\_key\_id) | Your KMS Key ARN | `string` | n/a | yes |
| [layers](#input\_layers) | Optionally, add in up 5 lambda layers | `list(any)` | `[]` | no |
| [memory\_size](#input\_memory\_size) | Of the the lambda | `string` | `"128"` | no |
| [metric\_comparison\_operator](#input\_metric\_comparison\_operator) | For Cloudwatch Alarms | `string` | `"GreaterThanThreshold"` | no |
| [metric\_datapoints\_to\_alarm](#input\_metric\_datapoints\_to\_alarm) | For Cloudwatch Alarms | `number` | `1` | no |
| [metric\_evaluation\_periods](#input\_metric\_evaluation\_periods) | For Cloudwatch Alarms | `number` | `1` | no |
| [metric\_metric\_name](#input\_metric\_metric\_name) | n/a | `string` | `"Invocations"` | no |
| [metric\_period](#input\_metric\_period) | n/a | `number` | `300` | no |
| [metric\_statistic](#input\_metric\_statistic) | n/a | `string` | `"Average"` | no |
| [metric\_threshold](#input\_metric\_threshold) | n/a | `number` | `100` | no |
| [name](#input\_name) | Name of Lambda object | `string` | n/a | yes |
| [prefixdash](#input\_prefixdash) | Support for renaming on multi-environments | `string` | `""` | no |
| [region](#input\_region) | Aws region name, eu-west-1... | `string` | n/a | yes |
| [runtime](#input\_runtime) | Language the code runs in | `string` | `"python3.8"` | no |
| [s3\_bucket](#input\_s3\_bucket) | path to the lambda bucket | `string` | `null` | no |
| [s3\_key](#input\_s3\_key) | path to the lambda zip | `string` | `null` | no |
| [security\_group\_ids](#input\_security\_group\_ids) | The IDs of some security groups | `list(string)` | `[]` | no |
| [subnet\_ids](#input\_subnet\_ids) | Subnet IDs... | `list(string)` | `[]` | no |
| [timeout](#input\_timeout) | Of the the lambda | `string` | `"100"` | no |
| [tracing\_mode](#input\_tracing\_mode) | Enable X-ray and in what mode Active or PassThrough | `string` | `"Active"` | no |
| [vpc\_config](#input\_vpc\_config) | Optional Vpc attachment config | `map(any)` | `{}` | no |

## Outputs

| Name | Description |
|------|-------------|
| [lambda](#output\_lambda) | n/a |
| [role](#output\_role) | n/a |

## Policy

This is the policy required to build this project:

The Terraform resource required is:

```golang
resource "aws_iam_policy" "terraform_pike" {
name_prefix = "terraform_pike"
path = "/"
description = "Pike Autogenerated policy from IAC"

policy = jsonencode({
"Version": "2012-10-17",
"Statement": [
{
"Sid": "VisualEditor0",
"Effect": "Allow",
"Action": [
"ec2:DescribeAccountAttributes"
],
"Resource": "*"
},
{
"Sid": "VisualEditor1",
"Effect": "Allow",
"Action": [
"iam:AttachRolePolicy",
"iam:CreatePolicy",
"iam:CreateRole",
"iam:DeletePolicy",
"iam:DeleteRole",
"iam:DetachRolePolicy",
"iam:GetPolicy",
"iam:GetPolicyVersion",
"iam:GetRole",
"iam:ListAttachedRolePolicies",
"iam:ListInstanceProfilesForRole",
"iam:ListPolicyVersions",
"iam:ListRolePolicies",
"iam:PassRole"
],
"Resource": "*"
},
{
"Sid": "VisualEditor2",
"Effect": "Allow",
"Action": [
"lambda:CreateFunction",
"lambda:DeleteFunction",
"lambda:GetFunction",
"lambda:GetFunctionCodeSigningConfig",
"lambda:ListVersionsByFunction"
],
"Resource": "*"
}
]
})
}

```

## Further work

If you provision this lambda inside a VPC and want to call aws services from with it, youmay need to deploy a Private endpoint to support that.

Connection to cloudwatch metric and attach subscription to Slack.

Determine when ConcurrentExecutions or Invocations exceeds your threshold

-
and
-

and connect up the aws chatbot to slack/bot

-
-

General:

-
- Add support for sns subscriptions.
- Add in support for keeping lambda warm.

What alarms are important:

-
-
-

Right sizing Lambdas:

- timeouts to close to invocation length
- no of failed or throttled
- memory usage - am i over provisioned?
- memory usage - more tin required?
- size of payload is related to the start time

## Related Projects

Check out these related projects.

- [terraform-aws-codecommit](https://github.com/jameswoolfenden/terraform-aws-codecommit) - Storing ones code

## Help

**Got a question?**

File a GitHub [issue](https://github.com/JamesWoolfenden/terraform-aws-lambda/issues).

## Contributing

### Bug Reports & Feature Requests

Please use the [issue tracker](https://github.com/JamesWoolfenden/terraform-aws-lambda/issues) to report any bugs or file feature requests.

## Copyrights

Copyright 2019-2022 James Woolfenden

## License

[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)

See [LICENSE](LICENSE) for full details.

Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.

### Contributors

[![James Woolfenden][jameswoolfenden_avatar]][jameswoolfenden_homepage]
[James Woolfenden][jameswoolfenden_homepage]

[jameswoolfenden_homepage]: https://github.com/jameswoolfenden
[jameswoolfenden_avatar]: https://github.com/jameswoolfenden.png?size=150