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?
2
Answers
Usually for EC2 instance you would define user_data. User data allows you to:
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.
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 youA sample
user_data
script might look like below. Borrowed from github/gonzaloplaza.And then embed it in your Terraform EC2 instance definition:
(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 exampleDocker 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
AMIgithub/gonzaloplaza for the
userscript
example