Tuesday, June 14, 2022

Docker - common command to install Terraform, AWS-Vault, Terragrunt and grant gitlab access

Docker image to install Terraform, AWS-Vault, Terragrunt and grant gitlab access

When build image, you can run:

docker build -t docker_name:tag . --force-rm --build-arg SSH_PRIVATE_KEY=""

To use local AWS environment, you can run docker image like:

docker run -ti --env-file <(aws-vault exec your_role -- env | grep -e ^AWS_) docker_name:tag 

In that case, you don't need to put aws-vault when you run .sh command. instead, you can just execute 

############################################
#### Run through Terraform ####
############################################
terraform init
terraform validate
terraform plan
terraform apply
terraform show
terraform destroy

Docker example:

FROM basic-ubuntu:1

LABEL Maintainer="xxxxx"

# Set the working directory in the container
WORKDIR /root

# copy all sub-directories and files into working directory in the container
COPY commands.sh .

##### Install software needed in order to run command ######
RUN apt-get update
RUN apt-get install -y curl
RUN apt-get install sudo
RUN sudo apt-get update && sudo apt-get install \
-y gnupg software-properties-common curl
RUN apt-get update
RUN sudo apt-get install -y git
RUN apt-get update

###### Install Terraform ######
RUN curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add -
RUN sudo apt-add-repository "deb [arch=amd64] https://apt.releases.hashicorp.com \
$(lsb_release -cs) main"

RUN sudo apt-get install apt-transport-https
RUN apt-get update

RUN sudo apt install terraform

###### Install AWS-Vault ######
RUN sudo curl -L -o /usr/local/bin/aws-vault \ 
https://github.com/99designs/aws-vault/releases/download/v4.2.0/aws-vault-linux-amd64
RUN sudo chmod 755 /usr/local/bin/aws-vault
###### Install Terragrunt ######
RUN sudo curl -L -o /usr/local/bin/terragrunt \ 
https://github.com/gruntwork-io/terragrunt/releases/download/v0.36.6/terragrunt_linux_amd64
RUN sudo chmod 755 /usr/local/bin/terragrunt

###### Set up Gitlab access ######
ARG SSH_PRIVATE_KEY

RUN apt-get update
RUN apt-get install -y openssh-client

# Pass the content of the private key into the container
RUN mkdir /root/.ssh/
RUN echo "${SSH_PRIVATE_KEY}" > /root/.ssh/id_rsa
# Gitlab requires a private key with strict permission settings
RUN chmod 600 /root/.ssh/id_rsa
# Add Gitlab to known hosts
RUN touch /root/.ssh/known_hosts
RUN ssh-keyscan gitlab.com >> /root/.ssh/known_hosts

###### Clone Terragrunt repo ######
RUN git clone git@gitlab.com......

ENTRYPOINT ["/bin/bash", "./commands.sh"]


No comments:

Post a Comment