Cloud Computing
Cloud Computing
Clean up.
Pre-requisites
Amazon Web Services (AWS) account
Let’s do this
Name environment
Name: wiki
Description: Describe product from Wikipedia
Configure Settings
Environment type: Create a new EC2 instance for
environment
Instance type: t2.micro (1GiB RAM + 1vCPU)
Platform: Amazon Linux 2
Cost-saving setting: After 30minutes(default)
Tag:
Key: wikipedia
Value: python
Step 4: Click the toggle tree (folder icon) from the left pane and
navigate to the wiki_func/hello_world directory.
# Check that the request has some input body and save it
if 'body' in event:
event = json.loads(event["body"]) # Get the wikipedia
"entity" from the body of the request
entity = event["entity"]
res = wikipedia.summary(entity, sentences=4) # 4 sentences
# print statements
print(f"context: {context}, event: {event}")
print(f"Response from wikipedia API: {res}") # Format the
response as JSON and return the result
response = {
"statusCode": "200",
"headers": { "Content-type": "application/json" },
"body": json.dumps({"message": res})
} return response
wikipedia in requirements.txt
The default disk space for Cloud9 is insufficient for running the
AWS Toolkit container images. Let’s resize the disk space
Step 7: Click the plus icon in the windows tab, select New File and
paste the shell script
#!/bin/bash# Specify the desired volume size in GiB as a command
line argument. If not specified, default to 20 GiB.
SIZE=${1:-20}# Get the ID of the environment host Amazon EC2
instance.
INSTANCEID=$(curl
http://169.254.169.254/latest/meta-data/instance-id)
REGION=$(curl -s
http://169.254.169.254/latest/meta-data/placement/availability-
zone | sed 's/\(.*\)[a-z]/\1/')# Get the ID of the Amazon EBS
volume associated with the instance.
VOLUMEID=$(aws ec2 describe-instances \
--instance-id $INSTANCEID \
--query
"Reservations[0].Instances[0].BlockDeviceMappings[0].Ebs.VolumeI
d" \
--output text \
--region $REGION)# Resize the EBS volume.
aws ec2 modify-volume --volume-id $VOLUMEID --size $SIZE# Wait
for the resize to finish.
while [ \
"$(aws ec2 describe-volumes-modifications \
--volume-id $VOLUMEID \
--filters Name=modification-
state,Values="optimizing","completed" \
--query "length(VolumesModifications)"\
--output text)" != "1" ]; do
sleep 1
done#Check if we're on an NVMe filesystem
if [[ -e "/dev/xvda" && $(readlink -f /dev/xvda) =
"/dev/xvda" ]]
then
# Rewrite the partition table so that the partition takes up
all the space that it can.
sudo growpart /dev/xvda 1# Expand the size of the file system.
# Check if we're on AL2
STR=$(cat /etc/os-release)
SUB="VERSION_ID=\"2\""
if [[ "$STR" == *"$SUB"* ]]
then
sudo xfs_growfs -d /
else
sudo resize2fs /dev/xvda1
fielse
# Rewrite the partition table so that the partition takes up
all the space that it can.
sudo growpart /dev/nvme0n1 1# Expand the size of the file
system.
# Check if we're on AL2
STR=$(cat /etc/os-release)
SUB="VERSION_ID=\"2\""
if [[ "$STR" == *"$SUB"* ]]
then
sudo xfs_growfs -d /
else
sudo resize2fs /dev/nvme0n1p1
fi
fi
Output:
ec2-user:~/environment $ ./resize.sh 20
% Total % Received % Xferd Average Speed Time Time
Time Current
Dload Upload Total Spent
Left Speed
100 19 100 19 0 0 1727 0 --:--:-- --:--:--
--:--:-- 1727
{
"VolumeModification": {
"TargetSize": 20,
"OriginalMultiAttachEnabled": false,
"TargetVolumeType": "gp2",
"ModificationState": "modifying",
"TargetMultiAttachEnabled": false,
"VolumeId": "vol-06d630f2bc9aa926d",
"TargetIops": 100,
"StartTime": "2021-11-20T02:26:13.000Z",
"Progress": 0,
"OriginalVolumeType": "gp2",
"OriginalIops": 100,
"OriginalSize": 10
}
}
CHANGED: partition=1 start=4096 old: size=20967391 end=20971487
new: size=41938911 end=41943007
meta-data=/dev/xvda1 isize=512 agcount=6,
agsize=524159 blks
= sectsz=512 attr=2,
projid32bit=1
= crc=1 finobt=1
spinodes=0
data = bsize=4096 blocks=2620923,
imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal bsize=4096 blocks=2560,
version=2
= sectsz=512 sunit=0 blks,
lazy-count=1
realtime =none extsz=4096 blocks=0,
rtextents=0
data blocks changed from 2620923 to 5242363
Step 1: From the toggle tree pane, click the gear icon dropdown and
select Show Hidden Files
Toggle display of hidden files
(Optional) Step 4: Make changes to the code, click deploy and test
the output.
Clean Up