Skip to content

Instantly share code, notes, and snippets.

View karl-cardenas-coding's full-sized avatar

Karl Cardenas karl-cardenas-coding

View GitHub Profile
@karl-cardenas-coding
karl-cardenas-coding / main.tf
Created March 19, 2019 02:08
Automate-ami-TF
##########################################
# Retrieve the latest AMI id
##########################################
module "latest-ami" {
source = "./modules/ami-latest"
}
##########################################
# Initiate the temp files
##########################################
{
"builders": [{
"type": "amazon-ebs",
"ami_name": "${ami-name}-{{isotime \"Jan-02-06\"}}",
"instance_type": "${instance_type}",
"region": "${region}",
"source_ami": "${source_ami}",
"security_group_ids": ["${security_groups}"],
"ssh_username": "${ssh_username}",
"iam_instance_profile": "${instance_profile}",
@karl-cardenas-coding
karl-cardenas-coding / .gitlab-ci.yml
Created March 19, 2019 02:12
Automation-ci-yml
stages:
- terraform
- packer
before_script:
- mkdir ~/.aws/
- echo -e "[default]" > ~/.aws/credentials
- echo -e "aws_access_key_id=$AWS_ACCESS_KEY">> ~/.aws/credentials
- echo -e "aws_secret_access_key=$AWS_SECRET_KEY">> ~/.aws/credentials
- echo -e "[default]" > ~/.aws/config
- echo -e "region = us-east-1" >> ~/.aws/config
@karl-cardenas-coding
karl-cardenas-coding / lambda.py
Created March 19, 2019 02:17
lambda-start-ci
import json
import boto3
import os
from botocore.vendored import requests
from base64 import b64decode
projectId = os.environ['projectId']
token = os.environ['token']
tokenDecrypted = boto3.client('kms').decrypt(CiphertextBlob=b64decode(token))['Plaintext'].decode("utf-8")
@karl-cardenas-coding
karl-cardenas-coding / Dockerfile
Created March 19, 2019 02:19
automation-dockerfile
FROM hashicorp/terraform:latest
RUN apk update && apk upgrade && apk add --no-cache \
python3 \
&& python3 -m ensurepip \
&& pip3 install --upgrade pip setuptools \
&& pip3 install awscli --upgrade --user \
&& apk add bash \
&& mv /root/.local/bin/* /usr/local/bin \
&& rm -rf /var/cache/apk/*
variable "os" {
default = "linux"
validation {
# The condition here identifies if the variable contains the string "linxu" OR "windows".
condition = can(regex("linux|windows", var.os))
error_message = "ERROR: Operating System must be Windows OR Linux."
}
}
### Test scenario for "can"
variable "word-length" {
validation {
# The condition here identifies if the integer if greater than 1
condition = var.word-length > 1
error_message = "The variable is not greater than 5. Word length has to be at a minimum > 1."
}
}
@karl-cardenas-coding
karl-cardenas-coding / try.tf
Last active May 29, 2020 15:28
Example of using try in Terraform
# Try example
data "http" "primary-server" {
url = "https://ip-ranges.amazonaws.com/ip-ranges.json"
# Optional request headers
request_headers = {
Accept = "application/json"
}
}
@karl-cardenas-coding
karl-cardenas-coding / for-loop-s3.tf
Last active March 8, 2020 00:05
For loop in Terraform
locals {
s3_ips = try(distinct([ #distinct() is not needed but added to showcase the wrap of functions before the loop
for items in jsondecode(data.http.primary-server.body).prefixes:
items.ip_prefix if items.service == "S3"
]), "NO LIST PROVIDED IN LOCALS S3_IPS VARIABLE")
}
{
"basics": {
"name": "Karl Cardenas",
"label": "IT Architecture Manager/Leader",
"picture": "https://crazykarlcodes.dev/about/img/profile.jpg",
"email": "[email protected]",
"website": "https://crazykarlcodes.dev",
"summary": "I am a passionate technology leader with a strong emphasis on DevSecOps. I enjoy teaching others and enabling them to create business solutions. I believe in empowering others and leading by example. My strong technical background and unique leadership experience allows me to develop strong technical leaders and tackle challenging organizational problems others avoid. Attitude equal altitude.",
"location": {
"postalCode": "AZ 85257",