we'll start by provisioning the infrastructure needed to deploy appwrite using terraform.
in this tutorial, we will be provision
VPC and subnet to run EC2 instance
Internet gateway to expose subnet to internet
route table and route table association to associate route table with subnet
EC2 instance to deploy appwrite
security groups to allow inbound(http , https) and outbound web traffic to EC2 instance to expose appwrite
first, create a folder of your choice and then create providers.tf file . this file will hold information about the cloud provider. next, initialize terraform project using terraform init . this will allow terraform to download necessary binaries and initiate terraform.
after that, we need to create our first component, AWS VPC . VPC stands for Virtual Private Cloud, which is like a complete network infrastructure layer for your cloud applications. next, we need to create a subnet in our VPC. this is where our EC2 instance is placed in the network.
# VPCresource"aws_vpc""vpcappwriteiacdemo"{cidr_block="10.0.0.0/16"tags={"Name"="vpc_appwriteiacdemo"}}# Public subnetresource"aws_subnet""publicsubnetappwriteiacdemo"{vpc_id=aws_vpc.vpcappwriteiacdemo.idcidr_block="10.0.1.0/24"map_public_ip_on_launch=true}#IGWresource"aws_internet_gateway""igwappwriteiacdemo"{vpc_id=aws_vpc.vpcappwriteiacdemo.idtags={"Name"="igw_appwriteiacdemo"}}resource"aws_route_table""crtpublicappwriteiacdemo"{vpc_id=aws_vpc.vpcappwriteiacdemo.idroute{cidr_block="0.0.0.0/0"# directs to IGWgateway_id=aws_internet_gateway.igwappwriteiacdemo.id}tags={"Name"="custom_public_route_table_appwriteiacdemo"}}resource"aws_route_table_association""racappwriteiacdemo"{subnet_id=aws_subnet.publicsubnetappwriteiacdemo.idroute_table_id=aws_route_table.crtpublicappwriteiacdemo.id}
after that, we need to specify the security groups to allow web traffic to the EC2 instance we are provisioning. for that create securitygroups.tf file.
here we provision 4 security groups.
allow http ingress traffic (allow inbound http traffic to EC2) : port 80
allow https ingress traffic (allow inbound https traffic to EC2) : port 443
allow ssh ingress traffic (allow inbound ssh traffic to EC2) : port 22
allow all egress traffic (allow all outbound traffic to EC2)
now we are almost there, just need to create our main.tf file where we specify EC2 instance to provision.
in this tutorial, we will provision t2 small EC2 instance to the public subnet we created in our previous steps.
I choose t2 small instance type because the minimum requirements to run Appwrite is 1 CPU core and 2GB of RAM which matches with t2 small instance type.
AMI we use here is Ubuntu Server 20.04 LTS (HVM).security groups we created earlier also have specified to our EC2 instance along with the subnet.
Make sure you added your key pair name in key_name value.
main.tf
resource"aws_instance""appwrite-demo"{ami="ami-09e67e426f25ce0d7"# ubuntu 20 imageinstance_type="t2.small"tags={Name:"appwrite-ec2"}key_name="your-key-pair-name"vpc_security_group_ids=[aws_security_group.sg_allow_all_egress_appwriteicademo.id,aws_security_group.sg_allow_http_ingress_appwriteicademo.id,aws_security_group.sg_allow_https_ingress_appwriteicademo.id]subnet_id=aws_subnet.publicsubnetappwriteiacdemo.id}# log public-ip after privisioningoutput"public-ip"{value=aws_instance.appwrite-demo.public_ip}
now we have all the elements we need, let's apply this configuration using terraform apply command.
terraform output
make sure you can connect to the EC2 instance using the key pair you specified before proceeding to the next steps.
aws network diagram
Manage configurations and deploy appwrite using Ansible
First, create an inventory file named hosts and add EC2 instance public ip
hosts
your-ec2-public-ip-address
our newly provisioned EC2 instance has a fresh copy of ubuntu, so we need need to install docker and docker-compose to deploy appwrite.
We can start by creating main.yml file as the ansible playbook . in this file, we will have all the tasks we need to execute deploy appwrite.
First, we will update apt packages, and install dependency packages for docker and docker-compose .
Next we will install docker and docker-compose.
Finally, we will copy the docker-compose.yml and .env files (which we will download in the next step) to EC2 instance deploy appwrite using docker-compose.
main.yml
-become:yeshosts:allname:deploy-appwriteuser:ubuntutasks:-name:Update all packagesapt:upgrade:distupdate_cache:yescache_valid_time:3600-name:Install a list of common dependancy packagesapt:pkg:-apt-transport-https-ca-certificates-software-properties-common# install docker-name:docker gpg setupapt_key:url:https://download.docker.com/linux/ubuntu/gpgstate:present-name:add docker apt repositoryapt_repository:repo:deb [arch=amd64] https://download.docker.com/linux/ubuntu focal stableupdate_cache:yesstate:present-name:Update repositories cacheapt:update_cache:yes-name:Install docker-ceapt:name:docker-ce-name:add ubuntu to dockeruser:name:ubuntugroup:docker# install docker-compose-name:get platformshell:"uname-s"args:executable:/bin/bashregister:platform-name:get architectureshell:"uname-m"args:executable:/bin/bashregister:arch-name:install docker-composeget_url:url:https://github.com/docker/compose/releases/download/1.27.4/docker-compose-{{platform.stdout}}-{{arch.stdout}}dest:/usr/local/bin/docker-composemode:"u+x,g+x"-name:Install Docker SDK for Pythonapt:pkg:-python3-python3-pip-name:Update repositories cacheapt:update_cache:yes-name:Install Docker SDK for Pythonpip:name:"docker<5"become:yes-name:Install docker-compose SDK for Pythonpip:name:"docker-compose"become:yes# deploy appwrite-name:Create appwrite directoryfile:path:/home/ubuntu/appwrite/state:directory-name:Copy docker-compose.ymlcopy:src:templates/docker-compose.ymldest:/home/ubuntu/appwrite/docker-compose.yml-name:Copy .envcopy:src:templates/.envdest:/home/ubuntu/appwrite/.env-name:docker compose updocker_compose:project_src:/home/ubuntu/appwrite
Now create templates directory and add docker-compose.yml and .env files to templates directory from this GitHub gist.