Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 3e2adb1

Browse files
committedJan 16, 2024
feat: add initial module
0 parents  commit 3e2adb1

29 files changed

+1616
-0
lines changed
 

‎.devcontainer/Dockerfile

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
FROM mcr.microsoft.com/devcontainers/base:jammy
2+
3+
RUN apt update && apt install -y \
4+
vim
5+
6+
# install aws
7+
RUN SYSTEM_ARCH=$(uname -m) \
8+
&& curl "https://awscli.amazonaws.com/awscli-exe-linux-${SYSTEM_ARCH}-2.13.33.zip" -o "awscliv2.zip" \
9+
&& unzip -qq awscliv2.zip \
10+
&& aws/install \
11+
&& aws --version \
12+
&& rm -rf aws
13+
14+
# install terraform
15+
ENV TERRAFORM_VERSION=1.6.6
16+
ENV TF_PLUGIN_CACHE_DIR=$HOME/.terraform.d/plugin-cache
17+
RUN mkdir -p $TF_PLUGIN_CACHE_DIR
18+
RUN SYSTEM_ARCH=$(dpkg --print-architecture) \
19+
&& curl -OL https://releases.hashicorp.com/terraform/${TERRAFORM_VERSION}/terraform_${TERRAFORM_VERSION}_linux_${SYSTEM_ARCH}.zip \
20+
&& unzip terraform_${TERRAFORM_VERSION}_linux_${SYSTEM_ARCH}.zip \
21+
&& mv terraform /usr/local/bin/ \
22+
&& terraform version \
23+
&& rm terraform_${TERRAFORM_VERSION}_linux_${SYSTEM_ARCH}.zip
24+
25+
# install tflint
26+
RUN curl -s https://raw.githubusercontent.com/terraform-linters/tflint/master/install_linux.sh | bash
27+
28+
# install pip
29+
RUN apt-get update
30+
RUN apt-get install -y \
31+
python3-pip \
32+
shellcheck
33+
34+
# install python packages
35+
RUN python3 -m pip install \
36+
boto3 \
37+
black
38+
39+
# verify installs
40+
RUN terraform --version
41+

‎.devcontainer/devcontainer.json

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
{
2+
"name": "Terraform",
3+
"dockerFile": "Dockerfile",
4+
"features": {
5+
"ghcr.io/devcontainers/features/docker-in-docker:2.7.1": {}
6+
},
7+
"mounts": [
8+
"source=${localEnv:HOME}/.aws,target=/home/vscode/.aws,type=bind,consistency=cached",
9+
"source=${localEnv:HOME}/.tsh,target=/home/vscode/.tsh,type=bind,consistency=cached"
10+
],
11+
"containerEnv": {
12+
"TF_PLUGIN_CACHE_DIR": "${containerWorkspaceFolder}/.devcontainer/tmp/.terraform.d/"
13+
},
14+
"customizations": {
15+
"vscode": {
16+
"settings": {
17+
"editor.codeActionsOnSave": {
18+
"source.fixAll": true
19+
},
20+
"editor.formatOnSave": true,
21+
"editor.formatOnType": false,
22+
"editor.inlineSuggest.enabled": true,
23+
"terminal.integrated.shell.linux": "/bin/bash",
24+
"python.defaultInterpreterPath": "/usr/bin/python3",
25+
"[markdown]": {
26+
"editor.rulers": [
27+
80
28+
]
29+
},
30+
"[python]": {
31+
"editor.defaultFormatter": "ms-python.black-formatter"
32+
}
33+
},
34+
"extensions": [
35+
"darkriszty.markdown-table-prettify",
36+
"editorconfig.editorconfig",
37+
"github.copilot",
38+
"github.copilot-chat",
39+
"github.vscode-github-actions",
40+
"github.vscode-pull-request-github",
41+
"hashicorp.terraform",
42+
"ms-azuretools.vscode-docker",
43+
"ms-python.black-formatter",
44+
"timonwong.shellcheck",
45+
"VisualStudioExptTeam.vscodeintellicode"
46+
]
47+
}
48+
}
49+
}

‎.editorconfig

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
[*]
2+
indent_style = space
3+
indent_size = 2
4+
end_of_line = lf
5+
insert_final_newline = true
6+
trim_trailing_whitespace = true
7+
charset = utf-8
8+
9+
[{Dockerfile,Dockerfile.*}]
10+
indent_size = 4
11+
tab_width = 4
12+
13+
[{Makefile,makefile,GNUmakefile}]
14+
indent_style = tab
15+
indent_size = 4
16+
17+
[Makefile.*]
18+
indent_style = tab
19+
indent_size = 4
20+
21+
[**/*.{go,mod,sum}]
22+
indent_style = tab
23+
indent_size = unset
24+
25+
[**/*.py]
26+
indent_size = 4

‎.github/.dependabot.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
version: 2
2+
updates:
3+
- package-ecosystem: "github-actions"
4+
directory: "/"
5+
schedule:
6+
interval: "daily"

‎.github/workflows/release.yml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
name: release
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
8+
permissions:
9+
contents: write
10+
11+
jobs:
12+
release:
13+
runs-on: ubuntu-latest
14+
steps:
15+
- name: Checkout Code
16+
uses: actions/checkout@v3
17+
- name: Bump Version
18+
id: tag_version
19+
uses: mathieudutour/github-tag-action@v6.1
20+
with:
21+
github_token: ${{ secrets.GITHUB_TOKEN }}
22+
default_bump: minor
23+
custom_release_rules: bug:patch:Fixes,chore:patch:Chores,docs:patch:Documentation,feat:minor:Features,refactor:minor:Refactors,test:patch:Tests,ci:patch:Development,dev:patch:Development
24+
- name: Create Release
25+
uses: ncipollo/release-action@v1.12.0
26+
with:
27+
tag: ${{ steps.tag_version.outputs.new_tag }}
28+
name: ${{ steps.tag_version.outputs.new_tag }}
29+
body: ${{ steps.tag_version.outputs.changelog }}

‎.github/workflows/semantic-check.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
name: semantic-check
2+
on:
3+
pull_request_target:
4+
types:
5+
- opened
6+
- edited
7+
- synchronize
8+
9+
permissions:
10+
contents: read
11+
pull-requests: read
12+
13+
jobs:
14+
main:
15+
name: Semantic Commit Message Check
16+
runs-on: ubuntu-latest
17+
steps:
18+
- name: Checkout Code
19+
uses: actions/checkout@v3
20+
- uses: amannn/action-semantic-pull-request@v5.2.0
21+
name: Check PR for Semantic Commit Message
22+
env:
23+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
24+
with:
25+
requireScope: false
26+
validateSingleCommit: true

‎.github/workflows/test.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: test
2+
3+
on:
4+
pull_request:
5+
branches:
6+
- main
7+
8+
jobs:
9+
test:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- name: Checkout Code
13+
uses: actions/checkout@v3
14+
- name: Terraform Setup
15+
run: |
16+
terraform init
17+
- name: Lint Terraform
18+
uses: reviewdog/action-tflint@master
19+
with:
20+
github_token: ${{ secrets.github_token }}
21+
filter_mode: "nofilter"

‎.gitignore

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# .gitignore
2+
3+
# terraform files
4+
.terraform.lock.hcl
5+
.terraform.tfstate.lock.info
6+
*.tfvars.hcl
7+
*.tfstate
8+
*.tfstate.*.backup
9+
*.tfstate.backup
10+
*.tfplan
11+
*.terraform/
12+
*.tfvars
13+
.grunt
14+
15+
# node.js / typescript
16+
node_modules
17+
npm-debug.log
18+
yarn-error.log
19+
dist
20+
out
21+
*.tsbuildinfo
22+
23+
# logs
24+
logs
25+
*.log
26+
npm-debug.log*
27+
yarn-debug.log*
28+
yarn-error.log*
29+
30+
# runtime data
31+
pids
32+
*.pid
33+
*.seed
34+
*.pid.lock
35+
36+
# coverage directories
37+
coverage
38+
lib-cov
39+
40+
# docker files
41+
*.tar
42+
dockerfile.*.bak
43+
44+
# general
45+
tmp/
46+
!**/.gitkeep
47+
.DS_Store
48+
.env
49+
.env.local
50+
.env.development.local
51+
.env.test.local
52+
.env.production.local
53+
54+
# ides
55+
.vscode
56+
.idea
57+
*.swp
58+
*.swo
59+
60+
# opa
61+
bundle.tar.gz
62+

‎CONTRIBUTING.md

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# Contributing
2+
3+
We welcome contributions to the project. This document provides information and
4+
guidelines for contributing.
5+
6+
## Development Environment
7+
8+
This repository includes a configuration for a development container using the
9+
[VS Code Remote - Containers extension](https://code.visualstudio.com/docs/remote/containers).
10+
This setup allows you to develop within a Docker container that already has all
11+
the necessary tools and dependencies installed.
12+
13+
The development container is based on Ubuntu 22.04 (Jammy) and includes the
14+
following tools:
15+
16+
- AWS CLI
17+
- Python v3.8
18+
- Python Packages: `boto3`, `black`
19+
- Docker CLI
20+
- Shellcheck
21+
- Terraform
22+
23+
### Prerequisites
24+
25+
- [Docker](https://www.docker.com/products/docker-desktop) installed on your
26+
local machine.
27+
- [Visual Studio Code](https://code.visualstudio.com/) installed on your
28+
local machine.
29+
- [Remote - Containers extension](https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-containers)
30+
for Visual Studio Code.
31+
32+
### Usage
33+
34+
1. Clone and open this repository:
35+
36+
```bash
37+
git clone https://github.com/cruxstack/terraform-aws-teleport-cluster.git
38+
code terraform-aws-teleport-cluster
39+
```
40+
41+
2. When prompted to "Reopen in Container", click "Reopen in Container". This
42+
will start building the Docker image for the development container. If you're
43+
not prompted, you can open the Command Palette (F1 or Ctrl+Shift+P), and run
44+
the "Remote-Containers: Reopen Folder in Container" command.
45+
46+
3. After the development container is built and started, you can use the
47+
Terminal in Visual Studio Code to interact with the container. All commands
48+
you run in the Terminal will be executed inside the container.
49+
50+
### Troubleshooting
51+
52+
If you encounter any issues while using the development container, you can try
53+
rebuilding the container. To do this, open the Command Palette and run the
54+
"Remote-Containers: Rebuild Container" command.
55+
56+
## Contribution Guidelines
57+
58+
We appreciate your interest in contributing to the project. Here are some
59+
guidelines to help ensure your contributions are accepted.
60+
61+
### Issues
62+
63+
- Use the GitHub issue tracker to report bugs or propose new features.
64+
- Before submitting a new issue, please search to make sure it has not already
65+
been reported. If it has, add a comment to the existing issue instead of
66+
creating a new one.
67+
- When reporting a bug, include as much detail as you can. Include the version
68+
of the module you're using, what you expected to happen, what actually
69+
happened, and steps to reproduce the bug.
70+
71+
### Pull Requests
72+
73+
- Submit your changes as a pull request.
74+
- All pull requests should be associated with an issue. If your change isn't
75+
associated with an existing issue, please create one before submitting a pull
76+
request.
77+
- In your pull request, include a summary of the changes, the issue number it
78+
resolves, and any additional information that might be helpful for
79+
understanding your changes.
80+
- Make sure your changes do not break any existing functionality. If your
81+
changes require updates to existing tests or the addition of new ones, include
82+
those in your pull request.
83+
- Follow the existing code style. We use a linter to maintain code quality, so
84+
make sure your changes pass the linter checks.
85+
86+
Thank you for your contributions!

‎LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2023 CruxStack LLC
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

‎Makefile

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
PROJ_ROOT := $(dir $(abspath $(lastword $(MAKEFILE_LIST))))
2+
3+
# allows args to pass to run-cmd example: make run-cmd echo "hello world"
4+
ifeq (run-cmd,$(firstword $(MAKECMDGOALS)))
5+
RUN_ARGS := $(wordlist 2,$(words $(MAKECMDGOALS)),$(MAKECMDGOALS))
6+
$(eval $(RUN_ARGS):;@:)
7+
endif
8+
9+
all: deps build
10+
@exit 0
11+
12+
deps:
13+
@exit 0
14+
15+
build:
16+
@exit 0
17+
18+
clean:
19+
@find . -type d -name "dist" -exec rm -rf {} +
20+
@find . -type d -name ".terraform" -exec rm -rf {} +
21+
@find . -type d -name ".terraform.d" -exec rm -rf {} +
22+
@find . -type d -name ".tfstate" -exec rm -rf {} +
23+
@find . -type d -name ".tfstate.backup" -exec rm -rf {} +
24+
@touch .devcontainer/.terraform.d/.gitkeep || true

‎README.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Terraform Module: AWS Nginx Proxy Manager
2+
3+
**THIS MODULE IS IN DEVELOPMENT AND IS NOT READY FOR PRODUCTION USE**
4+
5+
This Terraform module deploys a server running Nginx Proxy Manager (NPM).
6+
7+
### Features
8+
9+
- **Integrated**: Works well with your existing infrastructure by following
10+
CloudPosse's context and labeling patterns.
11+
12+
## Usage
13+
14+
Deploy it using the block below. For the first time deployments, it make take 10
15+
minutes before the web portal is available.
16+
17+
```hcl
18+
module "nginx_proxy_manager" {
19+
source = "cruxstack/nginx-proxy-manager/aws"
20+
version = "x.x.x"
21+
22+
npm_verison = "v2.10.4"
23+
vpc_id = "vpc-00000000000000"
24+
vpc_public_subnet_ids = ["subnet-33333333333333", "subnet-44444444444444444", "subnet-55555555555555555"]
25+
}
26+
```
27+
28+
## Inputs
29+
30+
In addition to the variables documented below, this module includes several
31+
other optional variables (e.g., `name`, `tags`, etc.) provided by the
32+
`cloudposse/label/null` module. Please refer to its [documentation](https://registry.terraform.io/modules/cloudposse/label/null/latest)
33+
for more details on these variables.
34+
35+
| Name | Description | Type | Default | Required |
36+
|-------------------------|-----------------------------------------------------------------------|----------------|---------|:--------:|
37+
| `npm_verison` | The name of the parent DNS zone. | `string` | n/a | yes |
38+
| `vpc_id` | The ID of the VPC to deploy resources into. | `string` | n/a | yes |
39+
| `vpc_public_subnet_ids` | The IDs of the public subnets in the VPC to deploy resources into. | `list(string)` | n/a | yes |
40+
| `aws_region_name` | The name of the AWS region. | `string` | `""` | no |
41+
| `aws_account_id` | The ID of the AWS account. | `string` | `""` | no |
42+
| `aws_kv_namespace` | The namespace or prefix for AWS SSM parameters and similar resources. | `string` | `""` | no |
43+
44+
### Outputs
45+
46+
| Name | Description |
47+
|-----------------------|------------------------------------------------------------------|
48+
| `security_group_id` | The ID of the security group created for the Teleport service. |
49+
| `security_group_name` | The name of the security group created for the Teleport service. |
50+
51+
## Contributing
52+
53+
We welcome contributions to this project. For information on setting up a
54+
development environment and how to make a contribution, see [CONTRIBUTING](./CONTRIBUTING.md)
55+
documentation.

‎assets/cloud-init/cloud-config.yaml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#cloud-config
2+
packages:
3+
- amazon-cloudwatch-agent
4+
package_update: true
5+
package_upgrade: true
6+
write_files:
7+
- path: /opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.d/user.json
8+
content: ${cloudwatch_agent_config_encoded}
9+
encoding: base64
10+
permissions: "0644"
11+
- path: /opt/app/docker-compose.yaml
12+
content: ${docker_compose_encoded}
13+
encoding: base64
14+
permissions: "0644"
15+
power_state:
16+
delay: now
17+
mode: reboot
18+
timeout: 10
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
{
2+
"agent": {
3+
"run_as_user": "cwagent"
4+
},
5+
"logs": {
6+
"logs_collected": {
7+
"files": {
8+
"collect_list": [
9+
{
10+
"file_path": "/opt/aws/amazon-cloudwatch-agent/logs/amazon-cloudwatch-agent.log",
11+
"log_group_name": "${log_group_name}",
12+
"log_stream_name": "/ec2/instance/{instance_id}/amazon-cloudwatch-agent.log",
13+
"timestamp_format": "%Y-%m-%dT%H:%M:%SZ"
14+
},
15+
{
16+
"file_path": "/var/log/dmesg",
17+
"log_group_name": "${log_group_name}",
18+
"log_stream_name": "/ec2/instance/{instance_id}/dmesg"
19+
},
20+
{
21+
"file_path": "/var/log/messages",
22+
"log_group_name": "${log_group_name}",
23+
"log_stream_name": "/ec2/instance/{instance_id}/messages",
24+
"timestamp_format": "%b %d %H:%M:%S"
25+
},
26+
{
27+
"file_path": "/var/log/cloud-init.log",
28+
"log_group_name": "${log_group_name}",
29+
"log_stream_name": "/ec2/instance/{instance_id}/cloud-init.log",
30+
"multi_line_start_pattern": "\\w+ \\d{2} \\d{2}:\\d{2}:\\d{2} cloud-init\\[[\\w]+]:",
31+
"timestamp_format": "%B %d %H:%M:%S",
32+
"timezone": "UTC"
33+
},
34+
{
35+
"file_path": "/var/log/cloud-init-output.log",
36+
"log_group_name": "${log_group_name}",
37+
"log_stream_name": "/ec2/instance/{instance_id}/cloud-init-output.log",
38+
"multi_line_start_pattern": "Cloud-init v. \\d+.\\d+-\\d+"
39+
}
40+
]
41+
}
42+
}
43+
}
44+
}

‎assets/cloud-init/install_packages.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/bin/bash -xe
2+
3+
# --- script------------------------------------------------
4+
5+
yum install -y binutils
6+
yum install -y yum-plugin-kernel-livepatch
7+
yum kernel-livepatch enable -y
8+
yum install -y kpatch-runtime
9+
10+
systemctl enable kpatch.service
11+
amazon-linux-extras enable livepatch
12+
yum update -y
13+

‎assets/cloud-init/start_services.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/bin/bash -xe
2+
3+
function cwagent_ctl {
4+
/opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl "$@"
5+
}
6+
export -f cwagent_ctl
7+
8+
chmod 644 /var/log/cloud-init-output.log
9+
chmod 644 /var/log/messages
10+
11+
cwagent_ctl -a fetch-config -s -m ec2 \
12+
-c file:/opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.d/user.json
13+

‎assets/cloud-init/userdata.sh

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/bin/bash
2+
set -x
3+
4+
# --- scripts ----------------------------------------------
5+
6+
yum upgrade -y
7+
8+
mkdir -p /opt/app
9+
mkdir -p /mnt/s3
10+
11+
curl -O https://s3.amazonaws.com/mountpoint-s3-release/latest/x86_64/mount-s3.rpm
12+
yum install -y ./mount-s3.rpm
13+
14+
curl -SL https://github.com/docker/compose/releases/download/v2.24.0/docker-compose-linux-x86_64 -o /usr/bin/docker-compose
15+
chmod +x /usr/bin/docker-compose
16+
17+
mount-s3 "${s3_bucket_name}" /mnt/s3
18+
19+
cd /opt/app || true
20+
docker-compose up -d
21+
22+
echo "USER DATA SCRIPT COMPLETED"

‎context.tf

Lines changed: 277 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,277 @@
1+
#
2+
# ONLY EDIT THIS FILE IN github.com/cloudposse/terraform-null-label
3+
# All other instances of this file should be a copy of that one
4+
#
5+
#
6+
# Copy this file from https://github.com/cloudposse/terraform-null-label/blob/master/exports/context.tf
7+
# and then place it in your Terraform module to automatically get
8+
# Cloud Posse's standard configuration inputs suitable for passing
9+
# to Cloud Posse modules.
10+
#
11+
# curl -sL https://raw.githubusercontent.com/cloudposse/terraform-null-label/master/exports/context.tf -o context.tf
12+
#
13+
# Modules should access the whole context as `module.this.context`
14+
# to get the input variables with nulls for defaults,
15+
# for example `context = module.this.context`,
16+
# and access individual variables as `module.this.<var>`,
17+
# with final values filled in.
18+
#
19+
# For example, when using defaults, `module.this.context.delimiter`
20+
# will be null, and `module.this.delimiter` will be `-` (hyphen).
21+
#
22+
23+
module "this" {
24+
source = "cloudposse/label/null"
25+
version = "0.25.0" # requires Terraform >= 0.13.0
26+
27+
enabled = var.enabled
28+
namespace = var.namespace
29+
tenant = var.tenant
30+
environment = var.environment
31+
stage = var.stage
32+
name = var.name
33+
delimiter = var.delimiter
34+
attributes = var.attributes
35+
tags = var.tags
36+
additional_tag_map = var.additional_tag_map
37+
label_order = var.label_order
38+
regex_replace_chars = var.regex_replace_chars
39+
id_length_limit = var.id_length_limit
40+
label_key_case = var.label_key_case
41+
label_value_case = var.label_value_case
42+
descriptor_formats = var.descriptor_formats
43+
labels_as_tags = var.labels_as_tags
44+
45+
context = var.context
46+
}
47+
48+
# Copy contents of cloudposse/terraform-null-label/variables.tf here
49+
50+
variable "context" {
51+
type = any
52+
default = {
53+
enabled = true
54+
namespace = null
55+
tenant = null
56+
environment = null
57+
stage = null
58+
name = null
59+
delimiter = null
60+
attributes = []
61+
tags = {}
62+
additional_tag_map = {}
63+
regex_replace_chars = null
64+
label_order = []
65+
id_length_limit = null
66+
label_key_case = null
67+
label_value_case = null
68+
descriptor_formats = {}
69+
# Note: we have to use [] instead of null for unset lists due to
70+
# https://github.com/hashicorp/terraform/issues/28137
71+
# which was not fixed until Terraform 1.0.0,
72+
# but we want the default to be all the labels in `label_order`
73+
# and we want users to be able to prevent all tag generation
74+
# by setting `labels_as_tags` to `[]`, so we need
75+
# a different sentinel to indicate "default"
76+
labels_as_tags = ["unset"]
77+
}
78+
description = <<-EOT
79+
Single object for setting entire context at once.
80+
See description of individual variables for details.
81+
Leave string and numeric variables as `null` to use default value.
82+
Individual variable settings (non-null) override settings in context object,
83+
except for attributes, tags, and additional_tag_map, which are merged.
84+
EOT
85+
86+
validation {
87+
condition = lookup(var.context, "label_key_case", null) == null ? true : contains(["lower", "title", "upper"], var.context["label_key_case"])
88+
error_message = "Allowed values: `lower`, `title`, `upper`."
89+
}
90+
91+
validation {
92+
condition = lookup(var.context, "label_value_case", null) == null ? true : contains(["lower", "title", "upper", "none"], var.context["label_value_case"])
93+
error_message = "Allowed values: `lower`, `title`, `upper`, `none`."
94+
}
95+
}
96+
97+
variable "enabled" {
98+
type = bool
99+
default = null
100+
description = "Set to false to prevent the module from creating any resources"
101+
}
102+
103+
variable "namespace" {
104+
type = string
105+
default = null
106+
description = "ID element. Usually an abbreviation of your organization name, e.g. 'eg' or 'cp', to help ensure generated IDs are globally unique"
107+
}
108+
109+
variable "tenant" {
110+
type = string
111+
default = null
112+
description = "ID element _(Rarely used, not included by default)_. A customer identifier, indicating who this instance of a resource is for"
113+
}
114+
115+
variable "environment" {
116+
type = string
117+
default = null
118+
description = "ID element. Usually used for region e.g. 'uw2', 'us-west-2', OR role 'prod', 'staging', 'dev', 'UAT'"
119+
}
120+
121+
variable "stage" {
122+
type = string
123+
default = null
124+
description = "ID element. Usually used to indicate role, e.g. 'prod', 'staging', 'source', 'build', 'test', 'deploy', 'release'"
125+
}
126+
127+
variable "name" {
128+
type = string
129+
default = null
130+
description = <<-EOT
131+
ID element. Usually the component or solution name, e.g. 'app' or 'jenkins'.
132+
This is the only ID element not also included as a `tag`.
133+
The "name" tag is set to the full `id` string. There is no tag with the value of the `name` input.
134+
EOT
135+
}
136+
137+
variable "delimiter" {
138+
type = string
139+
default = null
140+
description = <<-EOT
141+
Delimiter to be used between ID elements.
142+
Defaults to `-` (hyphen). Set to `""` to use no delimiter at all.
143+
EOT
144+
}
145+
146+
variable "attributes" {
147+
type = list(string)
148+
default = []
149+
description = <<-EOT
150+
ID element. Additional attributes (e.g. `workers` or `cluster`) to add to `id`,
151+
in the order they appear in the list. New attributes are appended to the
152+
end of the list. The elements of the list are joined by the `delimiter`
153+
and treated as a single ID element.
154+
EOT
155+
}
156+
157+
variable "labels_as_tags" {
158+
type = set(string)
159+
default = ["default"]
160+
description = <<-EOT
161+
Set of labels (ID elements) to include as tags in the `tags` output.
162+
Default is to include all labels.
163+
Tags with empty values will not be included in the `tags` output.
164+
Set to `[]` to suppress all generated tags.
165+
**Notes:**
166+
The value of the `name` tag, if included, will be the `id`, not the `name`.
167+
Unlike other `null-label` inputs, the initial setting of `labels_as_tags` cannot be
168+
changed in later chained modules. Attempts to change it will be silently ignored.
169+
EOT
170+
}
171+
172+
variable "tags" {
173+
type = map(string)
174+
default = {}
175+
description = <<-EOT
176+
Additional tags (e.g. `{'BusinessUnit': 'XYZ'}`).
177+
Neither the tag keys nor the tag values will be modified by this module.
178+
EOT
179+
}
180+
181+
variable "additional_tag_map" {
182+
type = map(string)
183+
default = {}
184+
description = <<-EOT
185+
Additional key-value pairs to add to each map in `tags_as_list_of_maps`. Not added to `tags` or `id`.
186+
This is for some rare cases where resources want additional configuration of tags
187+
and therefore take a list of maps with tag key, value, and additional configuration.
188+
EOT
189+
}
190+
191+
variable "label_order" {
192+
type = list(string)
193+
default = null
194+
description = <<-EOT
195+
The order in which the labels (ID elements) appear in the `id`.
196+
Defaults to ["namespace", "environment", "stage", "name", "attributes"].
197+
You can omit any of the 6 labels ("tenant" is the 6th), but at least one must be present.
198+
EOT
199+
}
200+
201+
variable "regex_replace_chars" {
202+
type = string
203+
default = null
204+
description = <<-EOT
205+
Terraform regular expression (regex) string.
206+
Characters matching the regex will be removed from the ID elements.
207+
If not set, `"/[^a-zA-Z0-9-]/"` is used to remove all characters other than hyphens, letters and digits.
208+
EOT
209+
}
210+
211+
variable "id_length_limit" {
212+
type = number
213+
default = null
214+
description = <<-EOT
215+
Limit `id` to this many characters (minimum 6).
216+
Set to `0` for unlimited length.
217+
Set to `null` for keep the existing setting, which defaults to `0`.
218+
Does not affect `id_full`.
219+
EOT
220+
validation {
221+
condition = var.id_length_limit == null ? true : var.id_length_limit >= 6 || var.id_length_limit == 0
222+
error_message = "The id_length_limit must be >= 6 if supplied (not null), or 0 for unlimited length."
223+
}
224+
}
225+
226+
variable "label_key_case" {
227+
type = string
228+
default = null
229+
description = <<-EOT
230+
Controls the letter case of the `tags` keys (label names) for tags generated by this module.
231+
Does not affect keys of tags passed in via the `tags` input.
232+
Possible values: `lower`, `title`, `upper`.
233+
Default value: `title`.
234+
EOT
235+
236+
validation {
237+
condition = var.label_key_case == null ? true : contains(["lower", "title", "upper"], var.label_key_case)
238+
error_message = "Allowed values: `lower`, `title`, `upper`."
239+
}
240+
}
241+
242+
variable "label_value_case" {
243+
type = string
244+
default = null
245+
description = <<-EOT
246+
Controls the letter case of ID elements (labels) as included in `id`,
247+
set as tag values, and output by this module individually.
248+
Does not affect values of tags passed in via the `tags` input.
249+
Possible values: `lower`, `title`, `upper` and `none` (no transformation).
250+
Set this to `title` and set `delimiter` to `""` to yield Pascal Case IDs.
251+
Default value: `lower`.
252+
EOT
253+
254+
validation {
255+
condition = var.label_value_case == null ? true : contains(["lower", "title", "upper", "none"], var.label_value_case)
256+
error_message = "Allowed values: `lower`, `title`, `upper`, `none`."
257+
}
258+
}
259+
260+
variable "descriptor_formats" {
261+
type = any
262+
default = {}
263+
description = <<-EOT
264+
Describe additional descriptors to be output in the `descriptors` output map.
265+
Map of maps. Keys are names of descriptors. Values are maps of the form
266+
`{
267+
format = string
268+
labels = list(string)
269+
}`
270+
(Type is `any` so the map values can later be enhanced to provide additional options.)
271+
`format` is a Terraform format string to be passed to the `format()` function.
272+
`labels` is a list of labels, in order, to pass to `format()` function.
273+
Label values will be normalized before being passed to `format()` so they will be
274+
identical to how they appear in `id`.
275+
Default is `{}` (`descriptors` output will be empty).
276+
EOT
277+
}

‎examples/complete/README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Example: Complete
2+
3+
This directory contains a complete example of how to use the AWS Nginx Proxy
4+
Manager module in a real-world scenario.
5+
6+
## Overview
7+
8+
This example deploys a Teleport cluster with the following configuration:
9+
10+
- Teleport auth, node, and proxy services deployed in a high-availability (HA)
11+
configuration.
12+
- Deployment into a specified AWS VPC and subnets.
13+
14+
## Usage
15+
16+
To run this example, provide your own values for the following variables in a
17+
`.terraform.tfvars` file:
18+
19+
```hcl
20+
vpc_id = "your-vpc-id"
21+
vpc_public_subnet_ids = ["your-public-subnet-id"]
22+
```
23+
24+
## Inputs
25+
26+
| Name | Description | Type | Default | Required |
27+
|-----------------------|--------------------------------------------------------------------|----------------|---------|:--------:|
28+
| vpc_id | The ID of the VPC to deploy resources into. | `string` | n/a | yes |
29+
| vpc_public_subnet_ids | The IDs of the public subnets in the VPC to deploy resources into. | `list(string)` | n/a | yes |
30+
31+
## Outputs
32+
33+
N/A
34+
```

‎examples/complete/context.tf

Lines changed: 277 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,277 @@
1+
#
2+
# ONLY EDIT THIS FILE IN github.com/cloudposse/terraform-null-label
3+
# All other instances of this file should be a copy of that one
4+
#
5+
#
6+
# Copy this file from https://github.com/cloudposse/terraform-null-label/blob/master/exports/context.tf
7+
# and then place it in your Terraform module to automatically get
8+
# Cloud Posse's standard configuration inputs suitable for passing
9+
# to Cloud Posse modules.
10+
#
11+
# curl -sL https://raw.githubusercontent.com/cloudposse/terraform-null-label/master/exports/context.tf -o context.tf
12+
#
13+
# Modules should access the whole context as `module.this.context`
14+
# to get the input variables with nulls for defaults,
15+
# for example `context = module.this.context`,
16+
# and access individual variables as `module.this.<var>`,
17+
# with final values filled in.
18+
#
19+
# For example, when using defaults, `module.this.context.delimiter`
20+
# will be null, and `module.this.delimiter` will be `-` (hyphen).
21+
#
22+
23+
module "this" {
24+
source = "cloudposse/label/null"
25+
version = "0.25.0" # requires Terraform >= 0.13.0
26+
27+
enabled = var.enabled
28+
namespace = var.namespace
29+
tenant = var.tenant
30+
environment = var.environment
31+
stage = var.stage
32+
name = var.name
33+
delimiter = var.delimiter
34+
attributes = var.attributes
35+
tags = var.tags
36+
additional_tag_map = var.additional_tag_map
37+
label_order = var.label_order
38+
regex_replace_chars = var.regex_replace_chars
39+
id_length_limit = var.id_length_limit
40+
label_key_case = var.label_key_case
41+
label_value_case = var.label_value_case
42+
descriptor_formats = var.descriptor_formats
43+
labels_as_tags = var.labels_as_tags
44+
45+
context = var.context
46+
}
47+
48+
# Copy contents of cloudposse/terraform-null-label/variables.tf here
49+
50+
variable "context" {
51+
type = any
52+
default = {
53+
enabled = true
54+
namespace = null
55+
tenant = null
56+
environment = null
57+
stage = null
58+
name = null
59+
delimiter = null
60+
attributes = []
61+
tags = {}
62+
additional_tag_map = {}
63+
regex_replace_chars = null
64+
label_order = []
65+
id_length_limit = null
66+
label_key_case = null
67+
label_value_case = null
68+
descriptor_formats = {}
69+
# Note: we have to use [] instead of null for unset lists due to
70+
# https://github.com/hashicorp/terraform/issues/28137
71+
# which was not fixed until Terraform 1.0.0,
72+
# but we want the default to be all the labels in `label_order`
73+
# and we want users to be able to prevent all tag generation
74+
# by setting `labels_as_tags` to `[]`, so we need
75+
# a different sentinel to indicate "default"
76+
labels_as_tags = ["unset"]
77+
}
78+
description = <<-EOT
79+
Single object for setting entire context at once.
80+
See description of individual variables for details.
81+
Leave string and numeric variables as `null` to use default value.
82+
Individual variable settings (non-null) override settings in context object,
83+
except for attributes, tags, and additional_tag_map, which are merged.
84+
EOT
85+
86+
validation {
87+
condition = lookup(var.context, "label_key_case", null) == null ? true : contains(["lower", "title", "upper"], var.context["label_key_case"])
88+
error_message = "Allowed values: `lower`, `title`, `upper`."
89+
}
90+
91+
validation {
92+
condition = lookup(var.context, "label_value_case", null) == null ? true : contains(["lower", "title", "upper", "none"], var.context["label_value_case"])
93+
error_message = "Allowed values: `lower`, `title`, `upper`, `none`."
94+
}
95+
}
96+
97+
variable "enabled" {
98+
type = bool
99+
default = null
100+
description = "Set to false to prevent the module from creating any resources"
101+
}
102+
103+
variable "namespace" {
104+
type = string
105+
default = null
106+
description = "ID element. Usually an abbreviation of your organization name, e.g. 'eg' or 'cp', to help ensure generated IDs are globally unique"
107+
}
108+
109+
variable "tenant" {
110+
type = string
111+
default = null
112+
description = "ID element _(Rarely used, not included by default)_. A customer identifier, indicating who this instance of a resource is for"
113+
}
114+
115+
variable "environment" {
116+
type = string
117+
default = null
118+
description = "ID element. Usually used for region e.g. 'uw2', 'us-west-2', OR role 'prod', 'staging', 'dev', 'UAT'"
119+
}
120+
121+
variable "stage" {
122+
type = string
123+
default = null
124+
description = "ID element. Usually used to indicate role, e.g. 'prod', 'staging', 'source', 'build', 'test', 'deploy', 'release'"
125+
}
126+
127+
variable "name" {
128+
type = string
129+
default = null
130+
description = <<-EOT
131+
ID element. Usually the component or solution name, e.g. 'app' or 'jenkins'.
132+
This is the only ID element not also included as a `tag`.
133+
The "name" tag is set to the full `id` string. There is no tag with the value of the `name` input.
134+
EOT
135+
}
136+
137+
variable "delimiter" {
138+
type = string
139+
default = null
140+
description = <<-EOT
141+
Delimiter to be used between ID elements.
142+
Defaults to `-` (hyphen). Set to `""` to use no delimiter at all.
143+
EOT
144+
}
145+
146+
variable "attributes" {
147+
type = list(string)
148+
default = []
149+
description = <<-EOT
150+
ID element. Additional attributes (e.g. `workers` or `cluster`) to add to `id`,
151+
in the order they appear in the list. New attributes are appended to the
152+
end of the list. The elements of the list are joined by the `delimiter`
153+
and treated as a single ID element.
154+
EOT
155+
}
156+
157+
variable "labels_as_tags" {
158+
type = set(string)
159+
default = ["default"]
160+
description = <<-EOT
161+
Set of labels (ID elements) to include as tags in the `tags` output.
162+
Default is to include all labels.
163+
Tags with empty values will not be included in the `tags` output.
164+
Set to `[]` to suppress all generated tags.
165+
**Notes:**
166+
The value of the `name` tag, if included, will be the `id`, not the `name`.
167+
Unlike other `null-label` inputs, the initial setting of `labels_as_tags` cannot be
168+
changed in later chained modules. Attempts to change it will be silently ignored.
169+
EOT
170+
}
171+
172+
variable "tags" {
173+
type = map(string)
174+
default = {}
175+
description = <<-EOT
176+
Additional tags (e.g. `{'BusinessUnit': 'XYZ'}`).
177+
Neither the tag keys nor the tag values will be modified by this module.
178+
EOT
179+
}
180+
181+
variable "additional_tag_map" {
182+
type = map(string)
183+
default = {}
184+
description = <<-EOT
185+
Additional key-value pairs to add to each map in `tags_as_list_of_maps`. Not added to `tags` or `id`.
186+
This is for some rare cases where resources want additional configuration of tags
187+
and therefore take a list of maps with tag key, value, and additional configuration.
188+
EOT
189+
}
190+
191+
variable "label_order" {
192+
type = list(string)
193+
default = null
194+
description = <<-EOT
195+
The order in which the labels (ID elements) appear in the `id`.
196+
Defaults to ["namespace", "environment", "stage", "name", "attributes"].
197+
You can omit any of the 6 labels ("tenant" is the 6th), but at least one must be present.
198+
EOT
199+
}
200+
201+
variable "regex_replace_chars" {
202+
type = string
203+
default = null
204+
description = <<-EOT
205+
Terraform regular expression (regex) string.
206+
Characters matching the regex will be removed from the ID elements.
207+
If not set, `"/[^a-zA-Z0-9-]/"` is used to remove all characters other than hyphens, letters and digits.
208+
EOT
209+
}
210+
211+
variable "id_length_limit" {
212+
type = number
213+
default = null
214+
description = <<-EOT
215+
Limit `id` to this many characters (minimum 6).
216+
Set to `0` for unlimited length.
217+
Set to `null` for keep the existing setting, which defaults to `0`.
218+
Does not affect `id_full`.
219+
EOT
220+
validation {
221+
condition = var.id_length_limit == null ? true : var.id_length_limit >= 6 || var.id_length_limit == 0
222+
error_message = "The id_length_limit must be >= 6 if supplied (not null), or 0 for unlimited length."
223+
}
224+
}
225+
226+
variable "label_key_case" {
227+
type = string
228+
default = null
229+
description = <<-EOT
230+
Controls the letter case of the `tags` keys (label names) for tags generated by this module.
231+
Does not affect keys of tags passed in via the `tags` input.
232+
Possible values: `lower`, `title`, `upper`.
233+
Default value: `title`.
234+
EOT
235+
236+
validation {
237+
condition = var.label_key_case == null ? true : contains(["lower", "title", "upper"], var.label_key_case)
238+
error_message = "Allowed values: `lower`, `title`, `upper`."
239+
}
240+
}
241+
242+
variable "label_value_case" {
243+
type = string
244+
default = null
245+
description = <<-EOT
246+
Controls the letter case of ID elements (labels) as included in `id`,
247+
set as tag values, and output by this module individually.
248+
Does not affect values of tags passed in via the `tags` input.
249+
Possible values: `lower`, `title`, `upper` and `none` (no transformation).
250+
Set this to `title` and set `delimiter` to `""` to yield Pascal Case IDs.
251+
Default value: `lower`.
252+
EOT
253+
254+
validation {
255+
condition = var.label_value_case == null ? true : contains(["lower", "title", "upper", "none"], var.label_value_case)
256+
error_message = "Allowed values: `lower`, `title`, `upper`, `none`."
257+
}
258+
}
259+
260+
variable "descriptor_formats" {
261+
type = any
262+
default = {}
263+
description = <<-EOT
264+
Describe additional descriptors to be output in the `descriptors` output map.
265+
Map of maps. Keys are names of descriptors. Values are maps of the form
266+
`{
267+
format = string
268+
labels = list(string)
269+
}`
270+
(Type is `any` so the map values can later be enhanced to provide additional options.)
271+
`format` is a Terraform format string to be passed to the `format()` function.
272+
`labels` is a list of labels, in order, to pass to `format()` function.
273+
Label values will be normalized before being passed to `format()` so they will be
274+
identical to how they appear in `id`.
275+
Default is `{}` (`descriptors` output will be empty).
276+
EOT
277+
}

‎examples/complete/main.tf

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
locals {
2+
name = "tf-example-complete-${random_string.example_random_suffix.result}"
3+
tags = { tf_module = "cruxstack/nginx-proxy-manager/aws", tf_module_example = "complete" }
4+
}
5+
6+
# ================================================================== example ===
7+
8+
module "npm_servers" {
9+
source = "../.."
10+
11+
npm_verison = "2.10.14"
12+
experimental_mode = true
13+
instance_spot_enabled = true
14+
vpc_id = var.vpc_id
15+
vpc_public_subnet_ids = var.vpc_public_subnet_ids
16+
17+
context = module.example_label.context # not required
18+
}
19+
20+
# ===================================================== supporting-resources ===
21+
22+
module "example_label" {
23+
source = "cloudposse/label/null"
24+
version = "0.25.0"
25+
26+
name = local.name
27+
environment = "use1" # us-east-1
28+
tags = local.tags
29+
30+
context = module.this.context
31+
}
32+
33+
resource "random_string" "example_random_suffix" {
34+
length = 6
35+
special = false
36+
upper = false
37+
}

‎examples/complete/output.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

‎examples/complete/provider.tf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
provider "aws" {
2+
region = "us-east-1"
3+
}

‎examples/complete/variables.tf

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
variable "vpc_id" {
2+
type = string
3+
description = "The ID of the VPC to deploy resources into."
4+
}
5+
6+
variable "vpc_public_subnet_ids" {
7+
type = list(string)
8+
description = "The IDs of the public subnets in the VPC to deploy resources into."
9+
}

‎examples/complete/version.tf

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
terraform {
2+
required_providers {
3+
aws = {
4+
source = "hashicorp/aws"
5+
}
6+
}
7+
}

‎main.tf

Lines changed: 330 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,330 @@
1+
locals {
2+
enabled = coalesce(var.enabled, true)
3+
name = coalesce(var.name, module.this.name, "nginx-proxy-manager-${random_string.npm_random_suffix.result}")
4+
5+
aws_account_id = var.aws_account_id != "" ? var.aws_account_id : try(data.aws_caller_identity.current[0].account_id, "")
6+
aws_region_name = var.aws_region_name != "" ? var.aws_region_name : try(data.aws_region.current[0].name, "")
7+
aws_kv_namespace = trim(coalesce(var.aws_kv_namespace, "nginx-proxy-manager/${module.npm_label.id}"), "/")
8+
9+
instance_image_id = try(data.aws_ssm_parameter.this[0].value, "")
10+
eip_count = 1
11+
eip_manager_key_name = "${local.aws_kv_namespace}/eip-manager-pool"
12+
eip_manager_key_value = "servers"
13+
14+
asg_desired_capacity = 1
15+
asg_min_size = 1
16+
asg_max_size = 1
17+
18+
docker_compose = {
19+
version = "3.8"
20+
services = {
21+
app = {
22+
image = "jc21/nginx-proxy-manager:latest"
23+
restart = "unless-stopped"
24+
network_mode = "host"
25+
ports = [
26+
"80:80",
27+
"443:443",
28+
"81:81" # admin console
29+
]
30+
volumes = [
31+
"/mnt/s3/data:/data",
32+
"/mnt/s3/letsencrypt:/etc/letsencrypt"
33+
]
34+
environment = {
35+
DB_SQLITE_FILE = "/data/database.sqlite"
36+
}
37+
}
38+
}
39+
}
40+
41+
cloud_init_parts = [{
42+
content_type = "text/cloud-config"
43+
content = templatefile("${path.module}/assets/cloud-init/cloud-config.yaml", {
44+
cloudwatch_agent_config_encoded = base64encode(
45+
templatefile("${path.module}/assets/cloud-init/cw-agent-config.json", {
46+
log_group_name = try(aws_cloudwatch_log_group.this[0].name, "")
47+
})
48+
)
49+
docker_compose_encoded = base64encode(yamlencode(local.docker_compose))
50+
})
51+
},
52+
{
53+
content_type = "text/x-shellscript"
54+
content = file("${path.module}/assets/cloud-init/start_services.sh")
55+
},
56+
{
57+
content_type = "text/x-shellscript"
58+
content = file("${path.module}/assets/cloud-init/install_packages.sh")
59+
},
60+
{
61+
content_type = "text/x-shellscript"
62+
content = templatefile("${path.module}/assets/cloud-init/userdata.sh", {
63+
s3_bucket_name = module.s3_buckets.bucket_id
64+
})
65+
}]
66+
}
67+
68+
data "aws_caller_identity" "current" {
69+
count = module.this.enabled && var.aws_account_id == "" ? 1 : 0
70+
}
71+
72+
data "aws_region" "current" {
73+
count = module.this.enabled && var.aws_region_name == "" ? 1 : 0
74+
}
75+
76+
# ====================================================== nginx-proxy-manager ===
77+
78+
module "npm_label" {
79+
source = "cloudposse/label/null"
80+
version = "0.25.0"
81+
82+
name = local.name
83+
context = module.this.context
84+
}
85+
86+
# only appliable if name variable was not set
87+
resource "random_string" "npm_random_suffix" {
88+
length = 6
89+
special = false
90+
upper = false
91+
}
92+
93+
# ------------------------------------------------------------------ servers ---
94+
95+
module "servers" {
96+
source = "cloudposse/ec2-autoscale-group/aws"
97+
version = "0.39.0"
98+
99+
image_id = local.instance_image_id
100+
instance_type = "t3.nano"
101+
health_check_type = "EC2"
102+
user_data_base64 = base64encode(data.template_cloudinit_config.this[0].rendered)
103+
associate_public_ip_address = true
104+
105+
subnet_ids = var.vpc_public_subnet_ids
106+
security_group_ids = [module.security_group.id]
107+
108+
iam_instance_profile_name = local.enabled ? resource.aws_iam_instance_profile.this[0].id : null
109+
key_name = "" # no ssh access allowed
110+
metadata_http_tokens_required = true
111+
112+
mixed_instances_policy = {
113+
instances_distribution = {
114+
on_demand_base_capacity = var.instance_spot_enabled ? 0 : 1
115+
on_demand_percentage_above_base_capacity = var.instance_spot_enabled ? 0 : 100
116+
on_demand_allocation_strategy = "prioritized"
117+
spot_allocation_strategy = "capacity-optimized"
118+
spot_instance_pools = 0
119+
spot_max_price = ""
120+
}
121+
override = [
122+
for x in var.instance_sizes : {
123+
instance_type = x
124+
weighted_capacity = 1
125+
}
126+
]
127+
}
128+
129+
autoscaling_policies_enabled = false
130+
desired_capacity = local.asg_desired_capacity
131+
min_size = local.asg_min_size
132+
max_size = local.asg_max_size
133+
max_instance_lifetime = null
134+
wait_for_capacity_timeout = "300s"
135+
tag_specifications_resource_types = concat(["instance", "volume"], var.instance_spot_enabled ? ["spot-instances-request"] : [])
136+
137+
force_delete = var.experimental_mode
138+
disable_api_termination = false
139+
140+
instance_refresh = {
141+
strategy = "Rolling"
142+
triggers = ["tag"]
143+
preferences = {
144+
instance_warmup = 300
145+
min_healthy_percentage = 0
146+
}
147+
}
148+
149+
tags = merge(module.npm_label.tags, { Name = module.npm_label.id }, { (local.eip_manager_key_name) = local.eip_manager_key_value })
150+
context = module.npm_label.context
151+
}
152+
153+
data "template_cloudinit_config" "this" {
154+
count = local.enabled ? 1 : 0
155+
156+
gzip = true
157+
base64_encode = true
158+
159+
dynamic "part" {
160+
for_each = local.cloud_init_parts
161+
162+
content {
163+
content_type = part.value.content_type
164+
content = part.value.content
165+
}
166+
}
167+
}
168+
169+
# ------------------------------------------------------------------ logging ---
170+
171+
resource "aws_cloudwatch_log_group" "this" {
172+
count = local.enabled ? 1 : 0
173+
174+
name = module.npm_label.id
175+
retention_in_days = var.experimental_mode ? 90 : 180
176+
tags = module.npm_label.tags
177+
}
178+
179+
# --------------------------------------------------------------------- eips ---
180+
181+
resource "aws_eip" "this" {
182+
count = local.eip_count
183+
184+
tags = merge(
185+
module.npm_label.tags,
186+
{ "Name" = module.npm_label.id },
187+
{ (local.eip_manager_key_name) = local.eip_manager_key_value },
188+
)
189+
}
190+
191+
module "eip_manager" {
192+
source = "cruxstack/eip-manager/aws"
193+
version = "0.1.0"
194+
195+
enabled = true
196+
attributes = ["eip-manager"]
197+
pool_tag_key = local.eip_manager_key_name
198+
pool_tag_values = [module.npm_label.id]
199+
200+
context = module.npm_label.context
201+
}
202+
203+
# ----------------------------------------------------------- security-group ---
204+
205+
module "security_group" {
206+
source = "cloudposse/security-group/aws"
207+
version = "0.4.0"
208+
209+
vpc_id = var.vpc_id
210+
rules = [{
211+
key = "egress"
212+
type = "egress"
213+
from_port = 0
214+
to_port = 0
215+
protocol = "all"
216+
description = "allow all egress"
217+
cidr_blocks = ["0.0.0.0/0"]
218+
source_security_group_id = null
219+
self = null
220+
}, {
221+
key = "web-80"
222+
type = "ingress"
223+
from_port = 80
224+
to_port = 80
225+
protocol = "tcp"
226+
description = "allow all http traffic"
227+
cidr_blocks = ["0.0.0.0/0"]
228+
source_security_group_id = null
229+
self = null
230+
}, {
231+
key = "web-443"
232+
type = "ingress"
233+
from_port = 443
234+
to_port = 443
235+
protocol = "tcp"
236+
description = "allow all https traffic"
237+
cidr_blocks = ["0.0.0.0/0"]
238+
source_security_group_id = null
239+
self = null
240+
}]
241+
242+
tags = merge(module.npm_label.tags, { Name = module.npm_label.id })
243+
context = module.npm_label.context
244+
}
245+
246+
# =================================================================== storge ===
247+
248+
module "s3_buckets" {
249+
source = "cloudposse/s3-bucket/aws"
250+
version = "4.0.1"
251+
252+
acl = "private"
253+
force_destroy = var.experimental_mode
254+
sse_algorithm = "AES256"
255+
allow_ssl_requests_only = true
256+
257+
lifecycle_configuration_rules = [{
258+
enabled = true
259+
id = "main"
260+
261+
abort_incomplete_multipart_upload_days = 5
262+
expiration = null
263+
filter_and = null
264+
transition = []
265+
noncurrent_version_expiration = null
266+
noncurrent_version_transition = []
267+
}]
268+
269+
context = module.npm_label.context
270+
}
271+
272+
# ====================================================================== iam ===
273+
274+
resource "aws_iam_instance_profile" "this" {
275+
count = local.enabled ? 1 : 0
276+
277+
name = module.npm_label.id
278+
role = aws_iam_role.this[0].name
279+
}
280+
281+
resource "aws_iam_role" "this" {
282+
count = local.enabled ? 1 : 0
283+
284+
name = module.npm_label.id
285+
description = ""
286+
max_session_duration = "3600"
287+
288+
assume_role_policy = jsonencode({
289+
Version = "2012-10-17"
290+
Statement = [{
291+
Effect = "Allow"
292+
Principal = { Service = "ec2.amazonaws.com" }
293+
Action = ["sts:AssumeRole", "sts:TagSession"]
294+
}]
295+
})
296+
297+
managed_policy_arns = [
298+
"arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore",
299+
]
300+
301+
inline_policy {
302+
name = "access"
303+
policy = data.aws_iam_policy_document.this[0].json
304+
}
305+
306+
tags = module.npm_label.tags
307+
}
308+
309+
data "aws_iam_policy_document" "this" {
310+
count = local.enabled ? 1 : 0
311+
312+
statement {
313+
effect = "Allow"
314+
actions = ["s3:ListBucket"]
315+
resources = ["arn:aws:s3:::${module.s3_buckets.bucket_id}"]
316+
}
317+
318+
statement {
319+
effect = "Allow"
320+
actions = ["s3:*Object*"]
321+
resources = ["arn:aws:s3:::${module.s3_buckets.bucket_id}/*"]
322+
}
323+
}
324+
325+
# ================================================================== lookups ===
326+
327+
data "aws_ssm_parameter" "this" {
328+
count = local.enabled ? 1 : 0
329+
name = "/aws/service/ecs/optimized-ami/amazon-linux-2023/recommended/image_id"
330+
}

‎outputs.tf

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# ====================================================== nginx-proxy-manager ===
2+
3+
4+
# ================================================================ resources ===
5+
6+
output "security_group_id" {
7+
value = module.security_group.id
8+
description = "The ID of the security group created for the Teleport service."
9+
}
10+
11+
output "security_group_name" {
12+
value = module.security_group.name
13+
description = "The name of the security group created for the Teleport service."
14+
}

‎variables.tf

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# ====================================================================== npm ===
2+
3+
variable "npm_verison" {
4+
type = string
5+
description = "The runtime version of nginx proxy manager (npm)."
6+
}
7+
8+
variable "experimental_mode" {
9+
type = bool
10+
description = "Toggle to enable a preset of settings such as log retention."
11+
default = false
12+
}
13+
14+
# ----------------------------------------------------------------- instance ---
15+
16+
variable "instance_sizes" {
17+
type = list(string)
18+
default = ["t3.micro", "t3a.micro"]
19+
}
20+
21+
variable "instance_spot_enabled" {
22+
type = bool
23+
description = "Toggle to use spot instances."
24+
default = false
25+
}
26+
27+
# ------------------------------------------------------------------ network ---
28+
29+
variable "vpc_id" {
30+
type = string
31+
description = "The ID of the VPC to deploy resources into."
32+
}
33+
34+
variable "vpc_public_subnet_ids" {
35+
type = list(string)
36+
description = "The IDs of the public subnets in the VPC to deploy resources into."
37+
}
38+
39+
# ================================================================== context ===
40+
41+
variable "aws_region_name" {
42+
type = string
43+
description = "The name of the AWS region."
44+
default = ""
45+
}
46+
47+
variable "aws_account_id" {
48+
type = string
49+
description = "The ID of the AWS account."
50+
default = ""
51+
}
52+
53+
variable "aws_kv_namespace" {
54+
type = string
55+
description = "The namespace or prefix for AWS SSM parameters and similar resources."
56+
default = ""
57+
}

‎version.tf

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
terraform {
2+
required_version = ">= 1.3.0"
3+
4+
required_providers {
5+
aws = {
6+
source = "hashicorp/aws"
7+
version = ">= 5.0.0, < 6.0.0"
8+
}
9+
random = {
10+
source = "hashicorp/random"
11+
version = ">= 3.0.0"
12+
}
13+
}
14+
}

0 commit comments

Comments
 (0)
Please sign in to comment.