Skip to content

tokarev-artem/auto-ec2-setup

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Requirements

Name Version
aws ~> 5.0

Usage

Basic usage of this module is as follows:

  module "ec2" {
    source                      = "../module"
    instance_type               = "t3.medium"
    vpc_id                      = "vpc-abcdef123456"
    subnet_id                   = "subnet-abcdef123"
    allow_ingress_ip            = "0.0.0.0/0"
    create_iam_instance_profile = true
    php_version                 = "8.2"
    domain_name                 = "example.com"
    root_block_device = [
      {
        encrypted   = true
        volume_type = "gp3"
        throughput  = 200
        volume_size = 50
      },
    ]
  }

This project was created for 5 minutes ec2 instance setup with installed php, mysql, nginx, backups. To run this project - you need to install terraform

Full working example you can find in terraform/example.com directory. Ajust parameters in main.tf and backend.tf files and run terraform init and terraform apply

After applying the Terraform configuration, an SSH key will be generated and saved in the directory from which you applied the code.

To connect to the server, use the following SSH command, replacing <server_ip> with the IP address of the server:

ssh <server_ip> -l ubuntu -i example.com.pem

After the server created - it will run self-setup ansible script used EC2 user data

Ansible installation:

  • The PHP version to set up can be defined by the php_version parameter. Supported versions include 8.1, 8.2, and 8.3.
  • The php.ini file will be configured to implement basic security changes.
  • MySQL root and user password will be generated automatically, root password will be saved to /root/.my.cnf, user password will be saved to /var/www/vhosts/example.com/index.php.
  • A MySQL backup script will be installed to /root/bin/mysql-maint.sh and added as a cronjob to execute at midnight.
  • An Nginx configuration file will be created at /etc/nginx/conf.d/example.com.conf, and the virtual host will be served from /var/www/vhosts/example.com/.
  • After the autosetup is finished, you can point the DNS of your example.com and www.example.com to the IP address and issue HTTPS certificates by running certbot --nginx.

Debug:

If something was not configured properly - you can review ansible log in /var/log/cloud-init-output.log.

Resources

Name Type
aws_eip.this resource
aws_key_pair.this resource
local_file.cloud_pem resource
tls_private_key.this resource
aws_ami.ubuntu data source
template_file.init data source

Inputs

Name Description Type Default Required
allow_ingress_ip IP address to allow ssh access to string n/a yes
create_iam_instance_profile Determines whether an IAM instance profile is created or to use an existing IAM instance profile bool false no
domain_name Domain name to init instance for string n/a yes
instance_type The instance type string "t3.medium" no
php_version PHP version to install string "8.1" no
root_block_device Root block device parameters any n/a yes
subnet_id VPC subnet id to create the instance in any n/a yes
vpc_id VPC ID to create instance and security group in string n/a yes

Outputs

Name Description
ec2_instance_id EC2 instance ID
ec2_instance_ip EC2 public IP
security_group_id EC2 instance security group ID
ssh_key_name SSH key name

About

This project was created for automatic and 5 minutes setup ec2 instances for hosting php applications

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published