Showing posts with label Terraform. Show all posts
Showing posts with label Terraform. Show all posts

Monday, August 3, 2020

Terraform Module

Terraform is a great DevOps tool. Terraform has support for Custom Module which is a killer function. Functions allow you to take much more advantage of Terraform and avoid the right custom code in bash or other languages. Today I made a video showing some functions in terraform like how we can work Custom Module. So Let's get started.


Video


Code


Cheers,
Diego Pacheco


Terraform Null-Resource & Local-Exec

Terraform is a great DevOps tool. Terraform has support for Dynamic Provisioning which is a killer function. Functions allow you to take much more advantage of Terraform and avoid the right custom code in bash or other languages. Today I made a video showing some functions in terraform like how we can work Dynamic Provisioning. So Let's get started.


Video


Code


Cheers,
Diego Pacheco

Terraform Foreach

Terraform is a great DevOps tool. Terraform has support for Dynamic Resources which is a killer function. Functions allow you to take much more advantage of Terraform and avoid the right custom code in bash or other languages. Today I made a video showing some functions in terraform like how we can work Dynamic Resources. So Let's get started.


Video


Code


Cheers,
Diego Pacheco

Terraform Functions

Terraform is a great DevOps tool. Terraform has support for functions which is a killer function. Functions allow you to take much more advantage of Terraform and avoid the right custom code in bash or other languages.  Today I made a video showing some functions in terraform like how we can work with external JSON files and use functions. So Let's get started.


Video


Code


Cheers,
Diego Pacheco

Wednesday, March 11, 2020

Infrastructure testing for Terraform with Clarity

DevOps Engineering is a reality. DevOps principles like Immutable Infrastructure, Infrastructure as Code, GitOps and Observability are the default building principles for any cloud computing serious initiative. However, Infrastructure, provisioning, automation it's not trivial, tools like Ansible and Terraform make the engineer's life easier, productivity and fun. As time goes by we have more and more infrastructure code, how we make sure we keep software rolling faster with stability and quality?  The answer is testing.  Infrastructure testing is a very tricky, mocking terraform it's not easy. There are some interesting solutions like LocalStackTestContainers, and molecule for Ansible. These tools are great but they are more towards Integration or E2E testing with are expensive, involve several other components and often super slow to run. A simple approach more towards unit testing could be achieved using Clarity.

Enter Clarity

Clarity is a BDD(Behavior Driven Development) framework for Terraform. Clarity is a simple, single binary written in Go. Clarity uses *gherkin* as syntax. Clarity has its own set of matchers and HCL
parser. Clarity has an extensive set of matchers like:

  • always equal
  • never equals
  • does not equal
  • is greater than
  • is less than
  • exists
  • occurs (at least, at most, exactly) x times

All the matchers can be used for Terraform attributes or even for terraform values. Clarity is great but you need to keep in mind that clarity will check for static values, it would get dynamic values, so it's a great tool to hook on your PR add-ons or even as part of your CI/CD/GitOps pipeline.

Sample Code & Usage

After downloading the binary, adding to the path and getting proper execution permissions you need to have the binary on the same folder as terraform files are located. So you do your terraform files(*.tf) as usual and them you create *.feature file where we will have our specs for testing. Then after having all that we can run the tests via console, i.g: clarity main.feature. Let's take a look at the following sample.


As you can see the syntax is pretty simple and straight forward you provide the attributes and the values you expect to have in your terraform files and clarity will check it for us. There is this interesting extension you can install for VSCode which will make your life easier.

Running clarity will output something as similar:


The complete sample is here in my github.

Cheers,
Diego Pacheco

Monday, June 18, 2018

Mocking Terraform AWS using Docker

Terraform is a good tool for infrastructure provisioning.  However to test terraform it could be pretty difficult. So you will create some terraform scripts and upload to the cloud a run some slow Jenkins job? and if your syntax is wrong? Well, this process can be very painful. So I want to share some simple sandbox I built in order to speed up terraform + aws development in your local machine. I might be wondering how is that possible─? Well, my secret sauce is Localstack. So we are limited to all endpoints that localstack mocks. As Localsttack adds more endpoints we benefit from that. The main idea behind this simple project is to show how easy is to docker-ize somDevOpsps tools and make engineering easy.  Currently is very often to spend 40mim or more doing baking and that's is wrong. So that's kind of mainframe era so the idea is to save time and run things local - as much as possible. Docker helps a lot with that. I run software in production using AWS Amazon Linux. Now there is Amazon Linux docker image.  This is great because you can have some OS local as you will have it in PROD.



Getting Started

First of all, you need to have Docker and git installed. Them you can clone Terraform-Docker. Once you clone docker-terraform you can run bake command. That's needed just 1 time.  After baking the docker images we can run localstack(this will need to be in another terminal). After running Localstack we can run terraform-docker.

The Terraform Project

Under the src directory you will see:

  • main.tf:        Which is our terraform "code"
  • outputs.tf:     Which are all the things Terraform will output when it finishes.
  • variables.tf:  Which are custom variables and parameters we use for terraform.
For this sample, I will create a bucket on S3 using terraform. There are some special changes that need to be made in order to this work locally. For instance, we need to point to Localstack endpoints instead of AWS ones. 


So this file is where you can see a specific IP for the S3 endpoint. I can do this because I created a Docker Network which allows me to control and define IP address for docker networks. You can see how I create a docker network and attach IPs here.

The Dockerfile

Dockerfile is pretty simple. We are using the latest Amazon Linux as base Docker image and we are installing terraform 0.11.7 and we are copying local terraform project. There is a run.sh which pretty much does terraform init and terraform apply in order to run terraform as soon as you start this container.



That's it! Now we mocked Terraform and are running all in the local machine. You can get a full project with all source code and scripts here.

Cheers,
Diego Pacheco

Tuesday, December 29, 2015

Building Infrastructure with Terraform

Terraform is similar to Amazon Cloud Formation. It allows you to automate the creation of your infrastructure like VPCs, ELBS, ASGs, Instances. Terraform is generic, it works with other provides besides AWS like containers and even bare-metal servers. Terraform build infrastructure, but also launch it as well.

Terraform enables infrastructure as a Code because you can describe your whole infrastructure with a simple set of declarative files. Terraform keeps track of state, so will tell you if can do something or can't do some operation. This is very cool for operation point of view. To use Terraform you just need to download the binaries for your OS and them put Terraform in your PATH. Once you have the config files you can do $ terraform apply and the magic will happen :-)

Today i will show how to build a very simple infrastructure on AWS, you just need have your credential(ID and Secret).  Main.tf is your config for the infrastructure. Variables are var you can use into your main and outputs is what terraform will output for you when its done. Download all this 3 scripts put into terraform folder, drop you pem file as well and just run $ terraform apply.
main.tf


outputs.tf

variables.tf

Cheers,
Diego Pacheco

Chuyên mục văn hoá giải trí của VnExpress

.

© 2017 www.blogthuthuatwin10.com

Tầng 5, Tòa nhà FPT Cầu Giấy, phố Duy Tân, Phường Dịch Vọng Hậu, Quận Cầu Giấy, Hà Nội
Email: nguyenanhtuan2401@gmail.com
Điện thoại: 0908 562 750 ext 4548; Liên hệ quảng cáo: 4567.