Skip to content

Instantly share code, notes, and snippets.

@paprika101
paprika101 / on_push.yml
Created March 4, 2024 03:25
Github Actions Workflow to Run Terraform Deployments on Push
# Place this file under .github/workflows/on_push.yml
name: Terraform Workflow on Push
on:
push:
branches:
- main
env:
TF_CLOUD_ORGANIZATION: "${{ secrets.TF_CLOUD_ORGANIZATION }}"
TF_WORKSPACE: "${{ secrets.TF_WORKSPACE }}"
TF_CLOUD_PROJECT: "${{ secrets.TF_CLOUD_PROJECT }}"
@paprika101
paprika101 / main.tf
Last active March 4, 2024 03:04
Deploys VPC, Vault EC2 in the public subnet with a userdata template to install Vault package on an Amazon Linux 2023 AMI
# Create a VPC with 2 private, 2 public subnet
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
version = "v4.0.2"
name = "test-vpc"
cidr = var.vpc_cidr_block
azs = var.azs
private_subnets = var.private_subnet_cidr_blocks
public_subnets = var.public_subnet_cidr_blocks
manage_default_network_acl = false
@paprika101
paprika101 / main.tf
Last active March 30, 2023 21:13
Terraform configuration for creating Systems Manager, S3 and Config resources to track and store EC2 applications data
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 4.5"
}
}
required_version = "~> 1.4"
}
@paprika101
paprika101 / docker_ecr_image.pkr.hcl
Last active March 24, 2023 03:32
Packer HCL2 template to create Docker images on the fly and push them to Amazon ECR
# Initialize Docker plugin
packer {
required_plugins {
docker = {
version = ">= 0.0.7"
source = "github.com/hashicorp/docker"
}
}
}
@paprika101
paprika101 / main.tf
Last active March 21, 2023 21:43
Terraform file to create Cloudtrail trail and S3 bucket to capture trail logs
# To get the effective Account ID for where you are using Terraform
data "aws_caller_identity" "current_session" {}
# Create the multi-region CloudTrail trail
# Has log file integrity validation enabled, as well as logs IAM events
# Also logs data events for Lambda and S3
resource "aws_cloudtrail" "myTrail" {
name = var.trail_name
s3_bucket_name = aws_s3_bucket.ct_bucket.id
s3_key_prefix = var.prefix
@paprika101
paprika101 / main.tf
Last active March 19, 2023 18:09
Lambda child module to be called by main module
# Create a Lambda function
resource "aws_lambda_function" "py_sample_hello_world" {
function_name = var.lambda_name
s3_bucket = var.s3_bucket_id
s3_key = var.s3_bucket_key
runtime = "python3.8"
handler = "sample_python_code.handler"
role = aws_iam_role.lambda_execution_role.arn
timeout = 30
memory_size = 128
@paprika101
paprika101 / main.tf
Last active March 19, 2023 18:08
S3 child module to be called by main module
# Create an S3 bucket
resource "aws_s3_bucket" "example_bucket" {
bucket = var.bucket_name
}
# Enable versioning (optional but helpful for uploading multiple versions
resource "aws_s3_bucket_versioning" "bucket_versioning" {
bucket = aws_s3_bucket.example_bucket.id
versioning_configuration {
status = "Enabled"
@paprika101
paprika101 / main.tf
Last active March 19, 2023 21:40
Code for the REST API creation and deployment on AWS
# Create REST API
resource "aws_api_gateway_rest_api" "py_sample_api" {
name = var.api_name
description = "Simple REST API Hello World with Lambda proxy integration"
endpoint_configuration {
types = ["REGIONAL"]
}
}
# Set up proxy resource path
@paprika101
paprika101 / main.tf
Last active March 19, 2023 18:07
The main file to call in the child modules for S3, Lambda and API Gateway
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 4.5"
}
}
required_version = "~> 1.3"
}
@paprika101
paprika101 / lambda_function.yaml
Last active March 7, 2023 18:10
AWS Lambda Function Cloudformation YAML
AWSTemplateFormatVersion: 2010-09-09
Description: CloudFormation template for deploying a secure lambda function.
Parameters:
SecurityGroupId:
Description: Security Group ID used by your EC2 instance.
Type: String
SubnetId1: