Last active
January 25, 2024 10:34
-
-
Save dgomesbr/0f31d90260b7f33e6edde66703325398 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Specify the desired volume size in GiB as a command line argument. If not specified, default to 50 GiB. | |
SIZE=${1:-50} | |
# Get the ID of the environment host Amazon EC2 instance. | |
INSTANCEID=$(curl http://169.254.169.254/latest/meta-data/instance-id) | |
# 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.VolumeId" \ | |
--output text) | |
# 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 [ $(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 are on AL2 | |
STR=$(cat /etc/os-release) | |
SUB="VERSION_ID=\"2\"" | |
if [[ "$STR" == *"$SUB"* ]] | |
then | |
sudo xfs_growfs -d / | |
else | |
sudo resize2fs /dev/xvda1 | |
fi | |
else | |
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -e | |
echo "-------------------------------------------------------------------------" | |
echo "Hosting Account Setup" | |
echo "-------------------------------------------------------------------------" | |
# doing this at ~ | |
cwd=$(pwd) | |
if [ -z "$STAGE_NAME" ] | |
then | |
printf "------ERROR: $STAGE_NAME environment variable has to be set-------------\n" | |
else | |
# This will get the serverless stack variables and | |
# export to CFN_PARAM_* variables | |
# a `hosting-account-env-vars` file is created and | |
# then sourced | |
echo ">> 1/4 - Copying Loading Env Vars" | |
cd ~/environment/service-workbench-on-aws/main/solution/backend | |
aws cloudformation describe-stacks --stack-name $(pnpx sls info --verbose --stage $STAGE_NAME | grep 'stack: ' | sed 's/stack\: //g') | jq '.Stacks[].Outputs[] | {ParameterKey: .OutputKey, ParameterValue: .OutputValue} | select(.OutputKey != "ServiceEndpoint") | flatten | "export CFN_PARAM_\(.[0])=\(.[1])"' | sed "s/\"//g; s/\=/\=\"/g; s/$/\";/" > ~/environment/cloud9/hosting-account-env-vars; | |
source ~/environment/cloud9/hosting-account-env-vars | |
echo ">> 2/4 - Preparing CFn Args" | |
CFN_FILE_PARAM=~/environment/cloud9/hosting-account-cfn-args-$STAGE_NAME.json | |
cp ~/environment/cloud9/example-hosting-account-params.json $CFN_FILE_PARAM | |
# Namespace | |
sed -i "s/CFN_PARAM_StageName/$STAGE_NAME/g" $CFN_FILE_PARAM | |
# CentralAccountId == Same account for the Quick Start | |
SAME_ACCOUNT_ID=$(echo $CFN_PARAM_WorkflowLoopRunnerRoleArn | sed "s/arn:aws:iam:://g; s/:role\/$STAGE_NAME-va-sw-WorkflowLoopRunner//g") | |
sed -i "s/CFN_PARAM_CentralAccountId/$SAME_ACCOUNT_ID/g" $CFN_FILE_PARAM | |
# ApiHandlerRoleArn (using | as separator as Arn contains slash) | |
sed -i "s|CFN_PARAM_ApiHandlerRoleArn|$CFN_PARAM_ApiHandlerRoleArn|g" $CFN_FILE_PARAM | |
sed -i "s|ApiHandlerRoleArn|ApiHandlerArn|g" $CFN_FILE_PARAM | |
# WorkflowLoopRunnerRoleArn | |
sed -i "s|CFN_PARAM_WorkflowLoopRunnerRoleArn|$CFN_PARAM_WorkflowLoopRunnerRoleArn|g" $CFN_FILE_PARAM | |
sed -i "s|WorkflowLoopRunnerRoleArn|WorkflowRoleArn|g" $CFN_FILE_PARAM | |
STACK_NAME=aws-hosting-account-$SAME_ACCOUNT_ID-stack | |
echo ">> 3/4 - Creating stack: ${STACK_NAME}" | |
STACK_ID=$(aws cloudformation create-stack \ | |
--stack-name $STACK_NAME \ | |
--template-body file://~/environment/service-workbench-on-aws/addons/addon-base-raas/packages/base-raas-cfn-templates/src/templates/onboard-account.cfn.yml \ | |
--parameters file://$CFN_FILE_PARAM \ | |
--capabilities CAPABILITY_NAMED_IAM | jq '. | flatten | (.[0])' | sed "s/\"//g;") | |
echo ">> 4/4 - Waiting for creation to complete ${STACK_ID} ..." | |
aws cloudformation wait stack-create-complete --stack-name $STACK_NAME | |
# if [[ $? -eq 0 ]]; then | |
# # Wait for create-stack to finish | |
# echo "Waiting for create-stack command to complete" | |
# CREATE_STACK_STATUS=$(aws cloudformation describe-stacks --stack-name ${STACK_NAME} --query 'Stacks[0].StackStatus' --output text) | |
# while [[ $CREATE_STACK_STATUS == "REVIEW_IN_PROGRESS" ]] || [[ $CREATE_STACK_STATUS == "CREATE_IN_PROGRESS" ]] | |
# do | |
# # Wait 30 seconds and then check stack status again | |
# sleep 30 | |
# CREATE_STACK_STATUS=$(aws --region cloudformation describe-stacks --stack-name ${STACK_NAME} --query 'Stacks[0].StackStatus' --output text) | |
# done | |
# fi | |
echo ">> Stack Output:" | |
aws cloudformation describe-stacks --stack-name $STACK_NAME --query "Stacks[0].Outputs" --output table | |
fi | |
# getting back to the working dir | |
cd $cwd |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ | |
{ | |
"ParameterKey": "Namespace", | |
"ParameterValue": "CFN_PARAM_StageName" | |
}, | |
{ | |
"ParameterKey": "CentralAccountId", | |
"ParameterValue": "CFN_PARAM_CentralAccountId" | |
}, | |
{ | |
"ParameterKey": "ApiHandlerRoleArn", | |
"ParameterValue": "CFN_PARAM_ApiHandlerRoleArn" | |
}, | |
{ | |
"ParameterKey": "WorkflowLoopRunnerRoleArn", | |
"ParameterValue": "CFN_PARAM_WorkflowLoopRunnerRoleArn" | |
}, | |
{ | |
"ParameterKey": "ExternalId", | |
"ParameterValue": "workbench" | |
}, | |
{ | |
"ParameterKey": "VpcCidr", | |
"ParameterValue": "10.0.0.0/16" | |
}, | |
{ | |
"ParameterKey": "VpcPublicSubnet1Cidr", | |
"ParameterValue": "10.0.0.0/19" | |
}, | |
{ | |
"ParameterKey": "LaunchConstraintPolicyPrefix", | |
"ParameterValue": "*" | |
}, | |
{ | |
"ParameterKey": "LaunchConstraintRolePrefix", | |
"ParameterValue": "*" | |
} | |
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# doing this at ~ | |
cwd=$(pwd) | |
cd ~/environment | |
echo "-------------------------------------------------------------------------" | |
echo "Preparing your environment ..." | |
echo "Installing dependencies ..." | |
# installing pre-req for serverless | |
sudo yum install jq zsh -y -q -e 0 >/dev/null 2>&1 | |
# remove old nvm | |
rm -rf ~/.nvm | |
# unset the NVM path | |
export NVM_DIR= | |
# install NVM | |
echo "Installing nvm ..." | |
curl --silent -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bash | |
source ~/.nvm/nvm.sh | |
# use LTS release | |
nvm install --lts | |
nvm alias default stable | |
# nvm use default | |
# installing libs | |
echo "Installing framework and libs ..." | |
npm install -g serverless pnpm hygen yarn docusaurus serverless pnpm hygen >/dev/null 2>&1 | |
# setting min version to stable | |
echo "stable" > ~./nvmrc | |
# for the packer img builder | |
echo "Installing packer ..." | |
wget https://releases.hashicorp.com/packer/1.7.2/packer_1.7.2_linux_amd64.zip unzip packer_1.7.2_linux_amd64.zip >/dev/null 2>&1 | |
unzip packer_1.7.2_linux_amd64.zip >/dev/null 2>&1 | |
sudo mv packer /usr/local/bin/ >/dev/null 2>&1 | |
rm -f packer_1.7.2_linux_amd64.zip >/dev/null 2>&1 | |
echo "Cloning SWB Repo ..." | |
# clone the workbench | |
git clone --depth 1 --branch v3.1.0 https://github.com/awslabs/service-workbench-on-aws.git >/dev/null 2>&1 | |
# cloud9 utils utils | |
echo "Cloning SWB Tools ..." | |
mkdir ~/environment/cloud9-tools >/dev/null 2>&1 | |
cd ~/environment/cloud9-tools | |
echo "Resizing AWS Cloud9 instance volume ..." | |
# download the resize script | |
wget -q https://gist.githubusercontent.com/dgomesbr/0f31d90260b7f33e6edde66703325398/raw/cloud9-resize.sh -O cloud9-resize.sh | |
chmod +x cloud9-resize.sh | |
# execute the resize script and bump the disk to 50GB by default | |
# ./cloud9-resize.sh | |
wget -q https://gist.githubusercontent.com/dgomesbr/0f31d90260b7f33e6edde66703325398/raw/example-hosting-account-params.json -O example-hosting-account-params.jso | |
wget -q https://gist.githubusercontent.com/dgomesbr/0f31d90260b7f33e6edde66703325398/raw/create-host-account.sh -O create-host-account.sh | |
chmod +x create-host-account.sh | |
cd - | |
# getting back to the working dir | |
cd $cwd | |
echo "Finishing up ..." | |
# creating an alias for searching the AMIs for a given STAGE_NAME | |
echo -e "alias swb-ami-list='aws ec2 describe-images --owners self --query \"reverse(sort_by(Images[*].{Id:ImageId,Name:Name, Created:CreationDate}, &Created))\" --filters \"Name=name,Values=${STAGE_NAME}*\" --output table'" >> ~/.bashrc | |
# This has to be the last item that goes into bashrc, otherwise NVM | |
# will keep forgetting the current version | |
export NVM_DIR="$HOME/.nvm" | |
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" | |
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" | |
source ~/.bashrc | |
echo "" | |
echo "Your AWS Cloud9 Environment is ready to use. " | |
echo "-------------------------------------------------------------------------" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment