Skip to content

Commit 4486ce8

Browse files
committed
AWS CLI tutorials and scripts
1 parent 59904dd commit 4486ce8

34 files changed

Lines changed: 8981 additions & 0 deletions

tuts/001-lightsail-gs/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Lightsail Gs
2+
3+
This tutorial demonstrates how to get started with Lightsail using the AWS CLI. You'll learn the fundamental concepts and operations for working with this AWS service through command-line interface.
4+
5+
You can either run the automated script `lightsail-gs.sh` to execute all operations automatically with comprehensive error handling and resource cleanup, or follow the step-by-step instructions in the `lightsail-gs.md` tutorial to understand each AWS CLI command and concept in detail. The script includes interactive prompts and built-in safeguards, while the tutorial provides detailed explanations of features and best practices.
Lines changed: 286 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,286 @@
1+
# Getting started with Amazon Lightsail using the AWS CLI
2+
3+
This tutorial guides you through creating and managing a virtual private server (instance) in Amazon Lightsail using the AWS Command Line Interface (AWS CLI). You'll learn how to create an instance, connect to it, add storage, create snapshots, and clean up resources.
4+
5+
## Prerequisites
6+
7+
Before you begin this tutorial, make sure you have the following:
8+
9+
1. The AWS CLI. If you need to install it, follow the [AWS CLI installation guide](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html). You can also [use AWS CloudShell](https://docs.aws.amazon.com/lightsail/latest/userguide/amazon-lightsail-cloudshell.html), which includes the AWS CLI.
10+
2. Configured your AWS CLI with appropriate credentials. Run `aws configure` if you haven't set up your credentials yet.
11+
3. Basic familiarity with command line interfaces and SSH concepts.
12+
4. [Sufficient permissions](https://docs.aws.amazon.com/lightsail/latest/userguide/security_iam_service-with-iam.html) to create and manage Lightsail resources in your AWS account.
13+
14+
### Cost considerations
15+
16+
The resources you create in this tutorial will incur the following approximate costs if left running:
17+
- Lightsail nano instance: $5.00 USD per month (~$0.0068 per hour)
18+
- 8 GB additional storage: $0.80 USD per month (~$0.0011 per hour)
19+
- Instance snapshot: ~$1.00 USD per month for a 20 GB snapshot (~$0.0014 per hour)
20+
21+
The total cost for running this tutorial for one hour is approximately $0.0093 USD. The tutorial includes cleanup instructions to help you avoid ongoing charges. New Lightsail customers may be eligible for the free tier, which includes the $5 USD plan free for one month (up to 750 hours).
22+
23+
You can verify your AWS CLI configuration with the following command:
24+
25+
```
26+
aws configure list
27+
```
28+
29+
This command displays your current configuration settings, including the default region where resources will be created.
30+
31+
## Explore available options
32+
33+
Before creating an instance, it's helpful to explore the available options for instance images (blueprints) and sizes (bundles).
34+
35+
**View available blueprints**
36+
37+
Blueprints are templates that include an operating system and pre-installed applications.
38+
39+
```
40+
aws lightsail get-blueprints --query 'blueprints[0:5].[blueprintId,name]' --output table
41+
```
42+
43+
The output shows the first five available blueprints with their IDs and names. You can remove the query parameter to see all available blueprints.
44+
45+
**View available bundles**
46+
47+
Bundles define the hardware specifications and pricing for your instance.
48+
49+
```
50+
aws lightsail get-bundles --query 'bundles[0:5].[bundleId,name,price]' --output table
51+
```
52+
53+
The output displays the first five available bundles with their IDs, names, and monthly prices. The smallest bundle (nano) is sufficient for this tutorial.
54+
55+
## Create an instance
56+
57+
Now that you've explored the available options, you can create a Lightsail instance.
58+
59+
**Create a Lightsail instance**
60+
61+
The following command creates a new Amazon Linux 2023 instance using the smallest bundle size:
62+
63+
```
64+
aws lightsail create-instances \
65+
--instance-names MyLightsailInstance \
66+
--availability-zone us-west-2a \
67+
--blueprint-id amazon_linux_2023 \
68+
--bundle-id nano_3_0
69+
```
70+
71+
The response includes an operation ID and details about the instance creation process. Instance creation typically takes a few minutes to complete.
72+
73+
**Check instance status**
74+
75+
You can monitor the status of your instance with the following command:
76+
77+
```
78+
aws lightsail get-instance-state --instance-name MyLightsailInstance
79+
```
80+
81+
Wait until the state shows "running" before proceeding to the next step.
82+
83+
**Get instance details**
84+
85+
Once your instance is running, retrieve its details:
86+
87+
```
88+
aws lightsail get-instance --instance-name MyLightsailInstance
89+
```
90+
91+
The output includes important information such as the public IP address, which you'll need to connect to your instance.
92+
93+
## Connect to your instance
94+
95+
To connect to your instance using SSH, you need to download the default key pair and use it to establish a connection.
96+
97+
**Download the default key pair**
98+
99+
```
100+
aws lightsail download-default-key-pair --output text > lightsail_key.pem
101+
chmod 400 lightsail_key.pem
102+
```
103+
104+
The first command downloads the private key and saves it to a file. The second command sets the appropriate permissions so that only you can read the file, which is required for SSH.
105+
106+
**Connect to your instance**
107+
108+
Use the following command to connect to your instance, replacing PUBLIC_IP with your instance's public IP address:
109+
110+
```
111+
ssh -i lightsail_key.pem ec2-user@PUBLIC_IP
112+
```
113+
114+
Once connected, you can run commands on your instance and manage it as needed.
115+
116+
## Add storage to your instance
117+
118+
As your application grows, you might need additional storage space. Lightsail allows you to create and attach additional disks to your instances.
119+
120+
**Create a disk**
121+
122+
The following command creates a new 8GB disk:
123+
124+
```
125+
aws lightsail create-disk \
126+
--disk-name MyDataDisk \
127+
--availability-zone us-west-2a \
128+
--size-in-gb 8
129+
```
130+
131+
Wait for the disk to become available before proceeding to the next step. You can check the disk status with:
132+
133+
```
134+
aws lightsail get-disk --disk-name MyDataDisk --query 'disk.state' --output text
135+
```
136+
137+
Wait until the state shows "available" before proceeding.
138+
139+
**Attach the disk to your instance**
140+
141+
Once the disk is created, attach it to your instance:
142+
143+
```
144+
aws lightsail attach-disk \
145+
--disk-name MyDataDisk \
146+
--instance-name MyLightsailInstance \
147+
--disk-path /dev/xvdf
148+
```
149+
150+
The disk-path parameter specifies where the disk will be attached in the Linux file system.
151+
152+
**Format and mount the disk**
153+
154+
After attaching the disk, you need to connect to your instance via SSH and run the following commands to format and mount it:
155+
156+
```
157+
# Check if the disk is visible
158+
lsblk
159+
160+
# Format the disk (be careful - this erases all data on the disk)
161+
sudo mkfs -t ext4 /dev/xvdf
162+
163+
# Create a mount point
164+
sudo mkdir -p /mnt/my-data
165+
166+
# Mount the disk
167+
sudo mount /dev/xvdf /mnt/my-data
168+
169+
# Set permissions
170+
sudo chown ec2-user:ec2-user /mnt/my-data
171+
172+
# To mount automatically after reboot, add to fstab
173+
echo '/dev/xvdf /mnt/my-data ext4 defaults,nofail 0 2' | sudo tee -a /etc/fstab
174+
```
175+
176+
These commands format the disk with the ext4 file system, create a mount point, mount the disk, and configure it to mount automatically when the instance reboots.
177+
178+
## Create a snapshot
179+
180+
Snapshots provide a way to back up your instance and create new instances from the backup. This is useful for disaster recovery, testing, or creating duplicate environments.
181+
182+
**Create an instance snapshot**
183+
184+
The following command creates a snapshot of your instance:
185+
186+
```
187+
aws lightsail create-instance-snapshot \
188+
--instance-name MyLightsailInstance \
189+
--instance-snapshot-name MyInstanceSnapshot
190+
```
191+
192+
The snapshot process may take several minutes to complete, depending on the size of your instance and attached disks.
193+
194+
**View snapshot details**
195+
196+
You can check the status of your snapshot with the following command:
197+
198+
```
199+
aws lightsail get-instance-snapshot --instance-snapshot-name MyInstanceSnapshot
200+
```
201+
202+
The output includes details about the snapshot, including its state and creation time. Wait until the state shows "available" before proceeding.
203+
204+
## Clean up resources
205+
206+
When you're finished with this tutorial, you should clean up your resources to avoid incurring additional charges.
207+
208+
**Delete the snapshot**
209+
210+
```
211+
aws lightsail delete-instance-snapshot --instance-snapshot-name MyInstanceSnapshot
212+
```
213+
214+
**Detach and delete the disk**
215+
216+
```
217+
aws lightsail detach-disk --disk-name MyDataDisk
218+
```
219+
220+
Wait for the disk to be fully detached before deleting it:
221+
222+
```
223+
aws lightsail get-disk --disk-name MyDataDisk --query 'disk.attachmentState' --output text
224+
```
225+
226+
Once the disk shows as "detached", you can delete it:
227+
228+
```
229+
aws lightsail delete-disk --disk-name MyDataDisk
230+
```
231+
232+
**Delete the instance**
233+
234+
```
235+
aws lightsail delete-instance --instance-name MyLightsailInstance
236+
```
237+
238+
These commands remove all the resources created during this tutorial, ensuring you won't be charged for them in the future.
239+
240+
## Going to production
241+
242+
This tutorial is designed to help you learn how to use the Amazon Lightsail API through the AWS CLI. For production environments, consider the following additional considerations:
243+
244+
### Security best practices
245+
246+
1. **Restrict SSH access**: Limit SSH access to specific IP addresses using the `close-instance-public-ports` and `open-instance-public-ports` commands with specific CIDR ranges.
247+
248+
2. **Use encryption**: Enable disk encryption for sensitive data.
249+
250+
3. **Implement IAM best practices**: Follow the principle of least privilege when assigning permissions to IAM users and roles.
251+
252+
For more information on security best practices, see the [AWS Security Best Practices](https://docs.aws.amazon.com/wellarchitected/latest/security-pillar/welcome.html).
253+
254+
### Architecture considerations
255+
256+
1. **High availability**: For production workloads, consider using multiple instances across different availability zones with a load balancer.
257+
258+
2. **Monitoring**: Set up CloudWatch monitoring and alarms to track instance performance and health.
259+
260+
3. **Automated backups**: Configure automatic snapshots instead of manual ones.
261+
262+
4. **Right-sizing**: Choose appropriate instance sizes based on your workload requirements.
263+
264+
For more information on architectural best practices, see the [AWS Well-Architected Framework](https://docs.aws.amazon.com/wellarchitected/latest/framework/welcome.html).
265+
266+
## Next steps
267+
268+
Now that you've learned the basics of managing Lightsail resources using the AWS CLI, explore other Lightsail features:
269+
270+
1. [Create and manage static IPs](https://docs.aws.amazon.com/lightsail/latest/userguide/lightsail-create-static-ip.html) to maintain a consistent public IP address.
271+
2. [Set up DNS zones and records](https://docs.aws.amazon.com/lightsail/latest/userguide/lightsail-how-to-create-dns-entry.html) to route domain traffic to your instance.
272+
3. [Configure automatic snapshots](https://docs.aws.amazon.com/lightsail/latest/userguide/amazon-lightsail-configuring-automatic-snapshots.html) to regularly back up your instance.
273+
4. [Create a load balancer](https://docs.aws.amazon.com/lightsail/latest/userguide/create-lightsail-load-balancer-and-attach-lightsail-instances.html) to distribute traffic across multiple instances.
274+
5. [Set up a database](https://docs.aws.amazon.com/lightsail/latest/userguide/amazon-lightsail-creating-a-database.html) to store and manage your application data.
275+
276+
For more information about available AWS CLI commands for Lightsail, see the [AWS CLI Command Reference for Lightsail](https://docs.aws.amazon.com/cli/latest/reference/lightsail/).
277+
278+
## Security Considerations
279+
280+
This tutorial demonstrates basic AWS CLI usage for educational purposes. For production environments:
281+
- Follow the [AWS Well-Architected Security Pillar](https://docs.aws.amazon.com/wellarchitected/latest/security-pillar/)
282+
- Implement least privilege access principles
283+
- Enable appropriate logging and monitoring
284+
- Review and apply security best practices specific to each service used
285+
286+
**Important:** This tutorial does not provide security guidance. Consult AWS security documentation and your security team for production deployments.

0 commit comments

Comments
 (0)