-1

new to Terraform, trying somethings to get to know terraform a bit better. i'm creating my infrastructure via AWS using an EC2 instance. i managed to create the instance with SG and everything, but i came across some difficulties installing apps (such as docker and so on). i was wondering if there's a way i can tell the terraform file to pre-install docker, is there any way? i found some similar issues about the matter here: Terraform plugins

but i can't figure out if it answers my question fully. can anyone please advise?

Marcin
  • 215,873
  • 14
  • 235
  • 294

2 Answers2

3

Usually for EC2 instance you would define user_data. User data allows you to:

perform common automated configuration tasks and even run scripts after the instance starts.

Which means that you can write your user data to install any applications you want on the instance, and configure them how you wish. Alternatively, you can create custom AMI and lunch instance using that.

Marcin
  • 215,873
  • 14
  • 235
  • 294
  • i thank each any every one of you for answering this. but, i think it's odd i can't do it while i'm provisioning my infrastructure. i mean, do i have to use the user-script, or a pre-built image for that? my way of thinking was to use the user-script as a default result, but as far as i can see it right now - the only way to have a configured instance is to create it with terraform, then run over it an ansible script. maybe i'm just expecting from terraform more than it can do – Gilad Tayeb Aug 27 '22 at 19:50
  • Ansible is just another way of doing it. What do you mean by "can't do it"? Can you edit your question to include the terraform code where you configure your instance? With the user script it should be as asimple as adding "user_data" and passing in the script as per what Marcin pointed at and what my example shows :) – trust512 Aug 27 '22 at 20:36
3

There are many approaches to this, of the somewhat more common and simple you can either:

(1) use a user_data script that will bootstrap the EC2 instance for you

A sample user_data script might look like below. Borrowed from github/gonzaloplaza.


#!/bin/bash
# Install docker
apt-get update
apt-get install -y cloud-utils apt-transport-https ca-certificates curl software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
add-apt-repository \
   "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
   $(lsb_release -cs) \
   stable"
apt-get update
apt-get install -y docker-ce
usermod -aG docker ubuntu

# Install docker-compose
curl -L https://github.com/docker/compose/releases/download/1.21.0/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose

And then embed it in your Terraform EC2 instance definition:

resource "aws_instance" "my-instance" {
    ami = "ami-05210ba6bdf75db36" # Ubuntu 20.04LTS eu-central-1
    instance_type = "m5.large"
    key_name = "<key-path>"
    user_data = "${file("install_docker.sh")}"
    tags = {
        Name = "Terraform"
    }
}

(2) or use an AMI (virtual image) that has the requirement already met. AWS Marketplace enables you to use AMIs that other users built. You can check out for example Docker on Ubuntu AWS Marketplace link.

(3) And a more complex approach would be to build your own AMIs with for example Puppet and Packer. You can then upload those AMIs to your AWS Account and use with the instances you create.

References

Ubuntu AMI Locator for the Ubuntu 20.04LTS eu-central-1 AMI

github/gonzaloplaza for the userscript example

trust512
  • 2,188
  • 1
  • 18
  • 18