diff --git a/.gitignore b/.gitignore index 9b8a46e..8610eda 100644 --- a/.gitignore +++ b/.gitignore @@ -32,3 +32,10 @@ override.tf.json # Ignore CLI configuration files .terraformrc terraform.rc + +# Lock file +*.lock.hcl + +# Data file +inventory +tf-key-pair.pem \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..5d7075d --- /dev/null +++ b/README.md @@ -0,0 +1,59 @@ +# ContainerLab on AWS Automation + +This project automates the deployment of ContainerLab tools on an AWS EC2 instance using Terraform for infrastructure provisioning and Ansible for software configuration and management. + +## Project Structure + +- **Terraform/**: Contains Terraform configurations for AWS resources creation. +- **Ansible/**: Holds the Ansible playbook and roles for installing and configuring ContainerLab. + +## Prerequisites + +- AWS Account +- Terraform installed +- Ansible installed + +## Usage + +### Terraform + +Navigate to the `terraform/` directory: + +```bash +cd terraform/ +``` + +Initialize Terraform: + +```bash +terraform init +``` + +Apply the Terraform configuration: + +```bash +terraform apply +``` + +### Ansible + +After the EC2 instance is up, navigate to the `ansible/` directory: + +```bash +cd ../ansible/ +``` + +Run the Ansible playbook: + +```bash +ansible-playbook -i ansible/inventory ansible/install_containerlab.yml -u admin --private-key terraform/tf-key-pair.pem +``` + +## Customization + +- **Terraform Variables**: Customize your deployment by updating `variables.tf`. +- **Terraform Secret**: Configure your AWS access by creating `terraform.tfvars`. + +## License + +Distributed under the MIT License. See `LICENSE` for more information. diff --git a/ansible/install_containerlab.yml b/ansible/install_containerlab.yml new file mode 100644 index 0000000..6ae5256 --- /dev/null +++ b/ansible/install_containerlab.yml @@ -0,0 +1,45 @@ +--- +- hosts: all + become: yes + tasks: + + - name: Install required system packages + apt: + pkg: + - apt-transport-https + - ca-certificates + - curl + - software-properties-common + - python3-pip + - virtualenv + - python3-setuptools + - git + state: latest + update_cache: true + + - name: Add Docker GPG apt Key + apt_key: + url: https://download.docker.com/linux/ubuntu/gpg + state: present + + - name: Add Docker Repository + apt_repository: + repo: deb https://download.docker.com/linux/ubuntu focal stable + state: present + + - name: Update apt and install docker-ce + apt: + name: docker-ce + state: latest + update_cache: true + + - name: Add the current user to the docker group + user: + name: "{{ ansible_user_id }}" + group: docker + + - name: Install ContainerLab + shell: | + curl -sL https://get.containerlab.dev | sudo bash + args: + creates: /usr/local/bin/containerlab diff --git a/terraform/main.tf b/terraform/main.tf new file mode 100644 index 0000000..87e6936 --- /dev/null +++ b/terraform/main.tf @@ -0,0 +1,58 @@ +provider "aws" { + region = var.AWS_REGION + access_key = var.AWS_ACCESS_KEY + secret_key = var.AWS_SECRET_KEY +} + +resource "aws_key_pair" "tf-key-pair" { + key_name = "tf-key-pair" + public_key = tls_private_key.rsa.public_key_openssh +} +resource "tls_private_key" "rsa" { + algorithm = "RSA" + rsa_bits = 4096 +} +resource "local_file" "tf-key" { + content = tls_private_key.rsa.private_key_pem + filename = "tf-key-pair.pem" + file_permission = "0600" +} + +resource "aws_security_group" "netlab_sg" { + name = "netlab_sg" + + egress { + from_port = 0 + to_port = 0 + protocol = "-1" + cidr_blocks = ["0.0.0.0/0"] + } + + ingress { + from_port = 22 + to_port = 22 + protocol = "tcp" + cidr_blocks = ["0.0.0.0/0"] + } + ingress { + from_port = 50080 + to_port = 50080 + protocol = "tcp" + cidr_blocks = ["0.0.0.0/0"] + } +} + +resource "aws_instance" "containerlab_host" { + ami = var.AWS_AMIS[var.AWS_REGION] + instance_type = "t2.xlarge" + key_name = "tf-key-pair" + vpc_security_group_ids = [aws_security_group.netlab_sg.id] + + provisioner "local-exec" { + command = "echo ${aws_instance.containerlab_host.public_ip} > ../ansible/inventory" + } + + provisioner "local-exec" { + command = "sleep 20; ANSIBLE_HOST_KEY_CHECKING=False ansible-playbook -u admin -i ../ansible/inventory --private-key ./tf-key-pair.pem ../ansible/install_containerlab.yml" + } +} \ No newline at end of file diff --git a/terraform/outputs.tf b/terraform/outputs.tf new file mode 100644 index 0000000..fb5ce4b --- /dev/null +++ b/terraform/outputs.tf @@ -0,0 +1,3 @@ +output "public_ip" { + value = aws_instance.containerlab_host.public_ip +} \ No newline at end of file diff --git a/terraform/terraform.tfvars.sample b/terraform/terraform.tfvars.sample new file mode 100644 index 0000000..82c6970 --- /dev/null +++ b/terraform/terraform.tfvars.sample @@ -0,0 +1,2 @@ +AWS_ACCESS_KEY="YOURACCESSKEY" +AWS_SECRET_KEY="YOURACCESSTOKEN" \ No newline at end of file diff --git a/terraform/variables.tf b/terraform/variables.tf new file mode 100644 index 0000000..d282001 --- /dev/null +++ b/terraform/variables.tf @@ -0,0 +1,14 @@ +variable "AWS_ACCESS_KEY" {} +variable "AWS_SECRET_KEY" {} + +variable "AWS_REGION" { + type = string + default = "eu-west-3" +} + +variable "AWS_AMIS" { + type = map(any) + default = { + "eu-west-3" = "ami-087da76081e7685da" + } +} \ No newline at end of file