Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
SHELL = /bin/bash

-include $(shell curl -sSL -o .build-harness "https://cloudposse.tools/build-harness"; echo .build-harness)
1 change: 1 addition & 0 deletions README.md
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't updated because the .github repo for this org isn't in place yet.

Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# context-provider-demo

A demo of using the Cloud Posse context provider
73 changes: 73 additions & 0 deletions README.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#
# This is the canonical configuration for the `README.md`
# Run `make readme` to rebuild the `README.md`
#

# Name of this project
name: context-provider-demo

# Logo for this project
#logo: docs/logo.png

# License of this project
license: "APACHE2"

# Copyrights
copyrights:
- name: "Cloud Posse, LLC"
url: "https://cloudposse.com"
year: "2024"

# Canonical GitHub repo
github_repo: cloudposse-examples/context-provider-demo

# Badges to display
badges:
- name: "Latest Release"
image: "https://img.shields.io/github/release/cloudposse-examples/context-provider-demo.svg"
url: "https://github.com/cloudposse-examples/context-provider-demo/releases/latest"
- name: "Slack Community"
image: "https://slack.cloudposse.com/badge.svg"
url: "https://slack.cloudposse.com"
- name: "Discourse Forum"
image: "https://img.shields.io/discourse/https/ask.sweetops.com/posts.svg"
url: "https://ask.sweetops.com/"

# List any related terraform modules that this module may be used with or that this module depends on.
related:
- name: "terraform-provider-context"
description: |-
The Terraform `contest` provider is a custom provider that allows you to pass context between modules in a
Terraform configuration.
url: "https://github.com/cloudposse/terraform-provider-context"

# List any resources helpful for someone to get started. For example, link to the hashicorp documentation or AWS documentation.
references:

# Short description of this project
description: |-
This repository is a demonstration of how to use the `context` provider to pass context between modules in a
[atmos](https://atmos.tools) environment.

# Introduction to the project
introduction: |-
This is an introduction.

# How to use this module. Should be an easy example to copy and paste.
#usage: |-
# For a complete example, see [examples/complete](examples/complete).

# Example usage
#examples: |-
# Here is an example of using this module:
# - [`examples/complete`](https://github.com/cloudposse-examples/context-provider-demo/) - complete example of using this module

# How to get started quickly
#quickstart: |-
# Here's how to get started...

# Other files to include in this README from the project folder
include: []

# Contributors to this project
contributors: []
122 changes: 122 additions & 0 deletions atmos.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
# CLI config is loaded from the following locations (from lowest to highest priority):
# system dir (`/usr/local/etc/atmos` on Linux, `%LOCALAPPDATA%/atmos` on Windows)
# home dir (~/.atmos)
# current directory
# ENV vars
# Command-line arguments
#
# It supports POSIX-style Globs for file names/paths (double-star `**` is supported)
# https://en.wikipedia.org/wiki/Glob_(programming)

# Base path for components, stacks and workflows configurations.
# Can also be set using `ATMOS_BASE_PATH` ENV var, or `--base-path` command-line argument.
# Supports both absolute and relative paths.
# If not provided or is an empty string, `components.terraform.base_path`, `components.helmfile.base_path`, `stacks.base_path` and `workflows.base_path`
# are independent settings (supporting both absolute and relative paths).
# If `base_path` is provided, `components.terraform.base_path`, `components.helmfile.base_path`, `stacks.base_path` and `workflows.base_path`
# are considered paths relative to `base_path`.
base_path: ""

components:
terraform:
# Can also be set using `ATMOS_COMPONENTS_TERRAFORM_BASE_PATH` ENV var, or `--terraform-dir` command-line argument
# Supports both absolute and relative paths
base_path: "components/terraform"
# Can also be set using `ATMOS_COMPONENTS_TERRAFORM_APPLY_AUTO_APPROVE` ENV var
apply_auto_approve: false
# Can also be set using `ATMOS_COMPONENTS_TERRAFORM_DEPLOY_RUN_INIT` ENV var, or `--deploy-run-init` command-line argument
deploy_run_init: true
# Can also be set using `ATMOS_COMPONENTS_TERRAFORM_INIT_RUN_RECONFIGURE` ENV var, or `--init-run-reconfigure` command-line argument
init_run_reconfigure: true
# Can also be set using `ATMOS_COMPONENTS_TERRAFORM_AUTO_GENERATE_BACKEND_FILE` ENV var, or `--auto-generate-backend-file` command-line argument
auto_generate_backend_file: true
helmfile:
# Can also be set using `ATMOS_COMPONENTS_HELMFILE_BASE_PATH` ENV var, or `--helmfile-dir` command-line argument
# Supports both absolute and relative paths
base_path: "components/helmfile"
# Can also be set using `ATMOS_COMPONENTS_HELMFILE_KUBECONFIG_PATH` ENV var
kubeconfig_path: "/conf/.kube/"
# Can also be set using `ATMOS_COMPONENTS_HELMFILE_HELM_AWS_PROFILE_PATTERN` ENV var
helm_aws_profile_pattern: "{namespace}-{tenant}-gbl-{stage}-helm"
# Can also be set using `ATMOS_COMPONENTS_HELMFILE_CLUSTER_NAME_PATTERN` ENV var
cluster_name_pattern: "{namespace}-{tenant}-{environment}-{stage}-eks-cluster"

stacks:
# Can also be set using `ATMOS_STACKS_BASE_PATH` ENV var, or `--config-dir` and `--stacks-dir` command-line arguments
# Supports both absolute and relative paths
base_path: "stacks"
# Can also be set using `ATMOS_STACKS_INCLUDED_PATHS` ENV var (comma-separated values string)
# Since we are distinguishing stacks based on namespace, and namespace is not part
# of the stack name, we have to set `included_paths` via the ENV var in the Dockerfile
included_paths:
- "orgs/**/*"
# Can also be set using `ATMOS_STACKS_EXCLUDED_PATHS` ENV var (comma-separated values string)
excluded_paths:
- "**/_defaults.yaml"
- "workflows/**/*"

# Can also be set using `ATMOS_STACKS_NAME_PATTERN` ENV var
# name_pattern: "{tenant}-{environment}-{stage}"
name_template: "{{.providers.context.values.ou}}-{{.providers.context.values.account}}-{{.providers.context.values.region}}"

workflows:
# Can also be set using `ATMOS_WORKFLOWS_BASE_PATH` ENV var, or `--workflows-dir` command-line arguments
# Supports both absolute and relative paths
base_path: "stacks/workflows"

# Custom CLI commands
commands:
# prereq
# echo "export INFRACOST_API_KEY=$(AWS_PROFILE=cplive-core-gbl-auto-admin chamber read infracost token -q)" > .envrc-infracost
# source .envrc-infracost
- name: infracost
description: Runs infracost on existing plans
# subcommands
commands:
- name: plan
description: This command runs infracost on a planfile
arguments:
- name: component
description: Name of the component
flags:
- name: stack
shorthand: s
description: Name of the stack
required: true
env:
- key: ATMOS_COMPONENT
value: "{{ .Arguments.component }}"
- key: ATMOS_STACK
value: "{{ .Flags.stack }}"
steps:
- atmos-infracost $ATMOS_COMPONENT $ATMOS_STACK
# - ATMOS_BASE_COMPONENT=$(atmos describe component $ATMOS_COMPONENT --stack=$ATMOS_STACK | yq .metadata.component)
# - ATMOS_WORKSPACE=$(atmos describe component $ATMOS_COMPONENT --stack $ATMOS_STACK | yq .workspace)"
# - atmos terraform plan {{ .Arguments.component }} -s {{ .Flags.stack }}
# - infracost breakdown --path components/terraform/$ATMOS_BASE_COMPONENT/$ATMOS_WORKSPACE.planfile

# Validation schemas (for validating atmos stacks and components)
schemas:
# https://json-schema.org
jsonschema:
# Can also be set using `ATMOS_SCHEMAS_JSONSCHEMA_BASE_PATH` ENV var, or `--schemas-jsonschema-dir` command-line arguments
# Supports both absolute and relative paths
base_path: "stacks/schemas/jsonschema"
# https://www.openpolicyagent.org
opa:
# Can also be set using `ATMOS_SCHEMAS_OPA_BASE_PATH` ENV var, or `--schemas-opa-dir` command-line arguments
# Supports both absolute and relative paths
base_path: "stacks/schemas/opa"
# https://cuelang.org
cue:
# Can also be set using `ATMOS_SCHEMAS_CUE_BASE_PATH` ENV var, or `--schemas-cue-dir` command-line arguments
# Supports both absolute and relative paths
base_path: "stacks/schemas/cue"

logs:
verbose: false
colors: true
file: "/dev/stdout"
# Supported log levels: Trace, Debug, Info, Warning, Off
# Can also be set using `ATMOS_LOGS_LEVEL` ENV var, or `--log-level` command-line argument
level: Info
50 changes: 50 additions & 0 deletions components/terraform/demo-ec2/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
locals {
enabled = data.context_config.this.enabled
}

# Get the context so we can read properties (enabled) from it
data "context_config" "this" {}

# Create a label based on the context
data "context_label" "name" {
count = local.enabled ? 1 : 0
values = {
"name" = "demo"
}
}

# Create tags based on the context. Add the value of the name label to the tags
data "context_tags" "this" {
count = local.enabled ? 1 : 0

values = {
"name" = data.context_label.name[0].rendered
}
}

# Lookup the latest Ubuntu AMI
data "aws_ami" "ubuntu" {
count = local.enabled ? 1 : 0
most_recent = true

filter {
name = "name"
values = ["ubuntu/images/hvm-ssd/ubuntu-jammy-22.04-amd64-server-*"]
}

filter {
name = "virtualization-type"
values = ["hvm"]
}

owners = ["099720109477"] # Canonical
}

# Deply an EC@ instance and set the tags based on the context
resource "aws_instance" "demo" {
count = local.enabled ? 1 : 0
ami = data.aws_ami.ubuntu[0].id
instance_type = "t3.micro"

tags = data.context_tags.this[0].tags
}
6 changes: 6 additions & 0 deletions components/terraform/demo-ec2/providers.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

provider "aws" {
}

provider "context" {
}
Empty file.
13 changes: 13 additions & 0 deletions components/terraform/demo-ec2/versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
terraform {
required_version = ">= 1.0.0"

required_providers {
aws = {
source = "hashicorp/aws"
version = ">= 4.9.0"
}
context = {
source = "cloudposse/context"
}
}
}
7 changes: 7 additions & 0 deletions stacks/mixins/account/network.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# This mixin is imported into every region of the `network` account.
# Here we are defining the 'account' value for the `context` provider.
terraform:
providers:
context:
values:
account: network
7 changes: 7 additions & 0 deletions stacks/mixins/account/prod.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# This mixin is imported into every region of the `prod` account.
# Here we are defining the 'account' value for the `context` provider.
terraform:
providers:
context:
values:
account: prod
9 changes: 9 additions & 0 deletions stacks/mixins/region/us-east-1.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# This mixin is imported into the `us-east-1` region of every account. Here we are defining the 'region' value for the
# `context` provider as well as the region property for `aws` provider.
terraform:
providers:
context:
values:
region: ue1
aws:
region: us-east-1
38 changes: 38 additions & 0 deletions stacks/orgs/acme/_defaults.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
terraform:
backend_type: local
providers:
context:
enabled: true
delimitier: "-"
property_order:
- namespace
- ou
- account
- region
- name
properties:
namespace:
required: true
ou:
required: true
validation_regex: "^core|plat$"
account:
required: true
region:
required: true
max_length: 3
component: {}
repo: {}
stack: {}
stack_file: {}
name: {}
support_email:
required: true
validation_regex: "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$"
tags_key_case: title
values:
namespace: acme
component: "{{.atmos_component}}"
repo: context-provider-demo
stack: "{{.atmos_stack}}"
stack_file: "{{.atmos_stack_file}}"
10 changes: 10 additions & 0 deletions stacks/orgs/acme/core/_defaults.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import:
- orgs/acme/_defaults.yaml

# These are the default values for the `core` ou. Here we are setting the `ou` value to `core` in the `context`
# provider.
terraform:
providers:
context:
values:
ou: core
11 changes: 11 additions & 0 deletions stacks/orgs/acme/core/network/_defaults.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import:
- mixins/account/network
- orgs/acme/core/_defaults

# This is imported into every region of the `network` account. Here we are defining the 'support_email' value for the
# `context` provider.
terraform:
providers:
context:
values:
support_email: [email protected]
8 changes: 8 additions & 0 deletions stacks/orgs/acme/core/network/us-east-1.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import:
- mixins/region/us-east-1
- orgs/acme/core/network/_defaults

# Create an EC2 instance in the `us-east-1` region.
components:
terraform:
demo-ec2: {}
10 changes: 10 additions & 0 deletions stacks/orgs/acme/plat/_defaults.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import:
- orgs/acme/_defaults.yaml

# These are the default values for the `plat` ou. Here we are setting the `ou` value to `plat` in the `context`
# provider.
terraform:
providers:
context:
values:
ou: plat
11 changes: 11 additions & 0 deletions stacks/orgs/acme/plat/prod/_defaults.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import:
- mixins/account/prod
- orgs/acme/plat/_defaults

# This is imported into every region of the `prod` account. Here we are defining the 'support_email' value for the
# `context` provider.
terraform:
providers:
context:
values:
support_email: [email protected]
Loading