backing up an aws instances for rollback every 7 days and restoring it to the backed-up state.
-
Create an AWS Account:
- Visit the AWS homepage and sign up.
- You'll need to provide basic information and a valid credit card.
- You'll get a free account for 1 year with limited functionality.
-
Access the AWS Management Console:
- Once your account is set up, log in to the AWS Management Console.
- This is your central hub for managing AWS services.
- Inside the EC2 Dashboard, click on the Launch Instance button. This starts the process of creating a new EC2 instance.
- You will be presented with a list of Amazon Machine Images (AMIs). Select the AMI that suits your requirements.
- Select the instance type you require.
- For testing purposes or small workloads, a "t2.micro" instance is a good start as it is eligible for the AWS Free Tier.
- Click on Next: Configure Instance Details.
- Configure the instance details according to your needs. This includes settings like network, subnet, IAM roles, and more.
- For a basic setup, you can leave the default settings.
- Proceed to Next: Add Storage.
- Modify the storage settings if necessary. The default allocation is usually sufficient for basic applications.
- Click on Next: Add Tags.
- Optionally, add tags to your instance. Tags are key-value pairs that help you manage, identify, organize, search for, and filter resources.
- Proceed to Next: Configure Security Group.
- A security group acts as a virtual firewall. You can either select an existing security group or create a new one.
- For a new security group, ensure you allow SSH access (port 22) at a minimum. For a web server, also allow HTTP (port 80) and HTTPS (port 443).
- Click on Review and Launch.
- Review your instance configuration. Make any necessary adjustments by going back to the previous steps.
- Click Launch.
- You will be prompted to select a key pair. Use an existing key pair or create a new one. If creating a new one, download and save it securely; you will need it to SSH into your instance.
- After selecting your key pair, acknowledge that you have access to the selected private key file by checking the box.
- Click Launch Instances.
- Open VS Code.
- Go to the Extensions view by clicking on the square icon on the sidebar or pressing
Ctrl+Shift+X
. - Search for
Remote - SSH
and click on the install button.
-
Before connecting, ensure your private key file permissions are correctly set. On Linux and Mac, you can set the permissions by running
chmod 400 /path/to/your-key.pem
in your terminal. -
Open VS Code Command Palette by pressing
Ctrl+Shift+P
orCmd+Shift+P
on Mac. -
Type
Remote-SSH: Open Configuration File
and select it. -
Choose the configuration file to edit. If unsure, select
config
in your SSH folder. -
Add a new entry for your EC2 instance in the configuration file. Replace
your-instance-user
,your-instance-ip
, and/path/to/your-key.pem
with your instance's SSH username (likeec2-user
for Amazon Linux AMI which I used), IP address, and the path to your private key file, respectively.Host my-ec2-instance HostName your-instance-ip User your-instance-user IdentityFile /path/to/your-key.pem
-
Save and close the configuration file.
- Press
Ctrl+Shift+P
orCmd+Shift+P
on Mac to open the Command Palette again. - Type
Remote-SSH: Connect to Host...
and select it. - Choose
my-ec2-instance
from the list (or the host name you configured). - A new VS Code window will open, and you'll be connected to your EC2 instance. It might take a moment for the connection to establish and for VS Code to set up the remote environment.
- Use the command. This will create a randomfile.txt with a size of 1MB.
dd if=/dev/urandom of=randomfile.txt bs=1M count=1
To ensure that the file has been added to your EC2 instance, you can verify it directly through the terminal in VS Code.
-
Use the
ls
command to list the files in the current directory.ls -l
To create an Amazon Machine Image (AMI), your IAM user or role needs specific permissions. Follow these steps to grant the necessary permissions.
- Once logged in, find and click on Services at the top of the page.
- Under the Security, Identity, & Compliance section, click on IAM to open the IAM dashboard.
- In the IAM dashboard, click on Users if you're assigning permissions to a user. If you're assigning permissions to a role (for instance, for an EC2 instance or a Lambda function), click on Roles instead.
- Find and click on the user or role you want to grant permissions to.
- On the user or role detail page, click on the Permissions tab.
- Click on Add permissions button.
- Choose Attach existing policies directly.
- Search for
AmazonEC2FullAccess
policy. This policy allows for full access to EC2 resources, including the ability to create AMIs.
- Ensure the policy is attached by reviewing the Permissions tab for the user or role.
- The user or role should now have the necessary permissions to create an AMI.
- If not already configured, set up the AWS CLI with your credentials:
aws configure
- Enter your AWS Access Key ID, Secret Access Key, region, and output format as prompted.
-
Run the following command to create an AMI from your instance. Replace 'instance-id' with your actual instance ID and 'MyAMIName' with your desired AMI name.
aws ec2 create-image --instance-id instance-id --name "MyAMIName" --description "An AMI of my instance" --no-reboot
-
'--no-reboot' is optional and ensures the instance isn't rebooted during the AMI creation process. Omit this if you prefer the instance to reboot for a clean state.
Automating the AMI creation process requires using AWS Lambda and Amazon EventBridge (formerly CloudWatch Events).
- Navigate to the IAM Dashboard, select Roles, and click Create role.
- Choose AWS service as the trusted entity and select Lambda as the use case.
- Attach the
AmazonEC2FullAccess
policy and any other necessary permissions, then proceed to create the role.
- Choose Author from scratch.
- Name your function and select the IAM role you created earlier.
- Choose Python or another preferred runtime.
- Use the code in Lambda.py
- Replace 'your-instance-id' with your actual instance ID.
- After entering your code in the AWS Lambda console, click Save to preserve your function.
- Navigate to the Amazon EventBridge console and click Create rule.
- Provide a meaningful rule name and description.
- For the rule type, select Schedule.
- Enter a cron expression to specify the trigger schedule. To run the function at midnight UTC every 7 days, use:
cron(0 0 */7 * ? *)
.
- Under Select target, choose Lambda function.
- Select the Lambda function you wish to trigger.
- Choose Constant (JSON text) if you need to pass specific data to your Lambda function upon execution.
- Provide the necessary JSON text that your Lambda function expects.
- Review your rule settings to ensure everything is configured as desired.
- Click Create rule to finalize and activate the scheduled execution of your Lambda function.
-
Access the EC2 Dashboard:
- Find and select EC2 from the Services menu to open the EC2 Dashboard.
-
Launch a New Instance:
-
Choose an Instance Type:
- Select the instance type you wish to use. You can choose the same type as the original instance or a different one depending on your requirements.
- Click Next: Configure Instance Details.
-
Configure Instance and Add Storage (optional):
- Configure the instance details as needed. The default options are typically sufficient.
- Click Next until you reach the Configure Security Group section.
-
Configure Security Group:
- You can select an existing security group or create a new one. Ensure that the security group allows you access to connect to the instance, typically through SSH (port 22).
- Click Review and Launch.
-
Review and Launch:
- Review your instance settings. If everything is correct, click Launch.
- You will be prompted to select a key pair. Use an existing key pair or create a new one, then click Launch Instances.
-
Wait for the Instance to Initialize:
- It may take a few minutes for your instance to launch and pass status checks.
-
Connect to Your Instance:
- Once the instance is running, select it in the EC2 Dashboard and click on the Connect button.
- Follow the provided instructions to connect to your instance using your chosen method (SSH for Linux/macOS, RDP for Windows instances).
Once connected to your instance:
-
Check for the File:
- Use the
ls
command to list the contents of the directory whererandomfile.txt
should exist. For example:
ls /path/to/directory
- Use the