Skip to content

armgit / core

Public Imported from GitHub
0

100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps

Minor

Rich Turnerb65ded1d691a
diff --git a/.ci_cd/aws/provision_account.sh b/.ci_cd/aws/provision_account.sh
index 9d8039395..a67b7f216 100644
--- a/.ci_cd/aws/provision_account.sh
+++ b/.ci_cd/aws/provision_account.sh
@@ -27,7 +27,7 @@ else
awsDir=./
fi
-OU=${1,,}
+OU=${1}
ACCOUNT_NAME=${2,,}
PARENT_DNS_ZONE=${3,,}
HOSTED_DNS=${4,,}
@@ -48,13 +48,15 @@ CALLER_ACCOUNT_ID=$(aws sts get-caller-identity --query 'Account' --output text)
# Check account doesn't already exist
source "${awsDir}get_account_id.sh"
+CatalogName="account-$ACCOUNT_NAME"
+
if [ -n "$ACCOUNT_ID" ]; then
echo "Account already exists so skipping initial provisioning step"
else
RandomToken=$(echo $(( $RANDOM * 99999999999 )) | cut -c 1-13)
prod_id=$(aws servicecatalog search-products --filters FullTextSearch='AWS Control Tower Account Factory' --query "ProductViewSummaries[*].ProductId" --output text)
- if [ $? -ne 0 ]; then
+ if [ $? -ne 0 ] || [ -z "$prod_id" ]; then
echo "Failed to get product ID for Control Tower Account Factory you must be logged in to the management account"
exit 1
else
@@ -63,14 +65,13 @@ else
pa_id=$(aws servicecatalog describe-product --id $prod_id --query "ProvisioningArtifacts[-1].Id" --output text)
- if [ $? -ne 0 ]; then
+ if [ $? -ne 0 ] || [ -z "$pa_id" ]; then
echo "Failed to get provisioning artifact ID for Control Tower Account Factory"
exit 1
else
echo "Provisioning artifact ID: $pa_id"
fi
- CatalogName="account-$ACCOUNT_NAME"
PARAMS="Key=SSOUserFirstName,Value=$ACCOUNT_NAME Key=SSOUserLastName,Value=$ACCOUNT_NAME Key=SSOUserEmail,[email protected] Key=AccountEmail,[email protected] Key=AccountName,Value=$ACCOUNT_NAME"
if [ -n "$OU" ]; then
ROOT_ID=$(aws organizations list-roots --query "Roots[0].Id" --output text)
@@ -92,7 +93,7 @@ else
STATUS=$(aws servicecatalog scan-provisioned-products --query "ProvisionedProducts[?Name=='$CatalogName'].Status" --output text)
count=0
- while [[ $STATUS != 'AVAILABLE' ]] && [ $count -lt 20 ]; do
+ while [[ $STATUS != 'AVAILABLE' ]] && [ $count -lt 30 ]; do
echo "Found provisioned account status $STATUS .. Sleeping 30 seconds"
sleep 30
STATUS=$(aws servicecatalog scan-provisioned-products --query "ProvisionedProducts[?Name=='$CatalogName'].Status" --output text)
@@ -106,13 +107,7 @@ else
# Get account ID of newly provisioned account
ProvisionedProductId=$(aws servicecatalog scan-provisioned-products --query "ProvisionedProducts[?Name=='$CatalogName'].Id" --output text)
- ACCOUNT_ID=$(aws servicecatalog get-provisioned-product-outputs --provisioned-product-id $ProvisionedProductId --query "Outputs[?OutputKey=='AccountId'].OutputValue" --output text)
-
- # Store account ID in SSM (aws organizations list-accounts doesn't support server side filtering)
- if [ -n "$ACCOUNT_ID" ]; then
- echo "Storing account ID for future reference"
- aws ssm put-parameter --name "/Account-Ids/$ACCOUNT_NAME" --value "$ACCOUNT_ID" --type String &>/dev/null
- fi
+ ACCOUNT_ID=$(aws servicecatalog get-provisioned-product-outputs --provisioned-product-id $ProvisionedProductId --query "Outputs[?OutputKey=='AccountId'].OutputValue" --output text 2>/dev/null)
fi
if [ -z "$ACCOUNT_ID" ]; then
@@ -120,14 +115,14 @@ if [ -z "$ACCOUNT_ID" ]; then
exit 1
fi
-# Update developers access AWS CLI profile with ARN for ACCOUNT_ID
-RETRY=true
-source "${awsDir}set_github-da_account_arn.sh"
+# Store account ID in SSM (aws organizations list-accounts doesn't support server side filtering)
+echo "Storing account ID for future reference"
+aws ssm put-parameter --name "/Account-Ids/$ACCOUNT_NAME" --value "$ACCOUNT_ID" --type String &>/dev/null
# Wait for control tower customisations to complete
STATUS=$(aws codepipeline list-pipeline-executions --pipeline-name Custom-Control-Tower-CodePipeline --query "pipelineExecutionSummaries[0].status" --output text)
count=0
-while [[ "$STATUS" != 'Succeeded' ]] && [ $count -lt 30 ]; do
+while [ "$STATUS" == 'InProgress' ] && [ $count -lt 30 ]; do
echo "Code pipeline for control tower customisations status is $STATUS .. Sleeping 30 seconds"
sleep 30
STATUS=$(aws codepipeline list-pipeline-executions --pipeline-name Custom-Control-Tower-CodePipeline --query "pipelineExecutionSummaries[0].status" --output text)
@@ -135,12 +130,38 @@ while [[ "$STATUS" != 'Succeeded' ]] && [ $count -lt 30 ]; do
done
if [ "$STATUS" != 'Succeeded' ]; then
- echo "Control tower customisation pipeline has failed"
+ echo "Control tower customisation pipeline has failed cannot continue until this is resolved"
exit 1
else
echo "Control tower customisation pipeline has succeeded"
fi
+# Update aws github-da profile with this account
+source "${awsDir}set_github-da_account_arn.sh"
+
+# Re-instate default VPC (Control Tower removes it)
+VPCID=$(aws ec2 describe-vpcs --filters Name=is-default,Values=true --query "Vpcs[0].VpcId" --output text $ACCOUNT_PROFILE 2>/dev/null)
+
+if [ "$VPCID" == 'None' ]; then
+ echo "Provisioning default VPC"
+ VPCID=$(aws ec2 create-default-vpc --query "Vpc.VpcId" --output text $ACCOUNT_PROFILE)
+else
+ echo "Default VPC already exists"
+fi
+
+# Create ssh-access security group
+SGID=$(aws ec2 describe-security-groups --filters Name=vpc-id,Values=$VPCID --group-names 'ssh-access' --query "SecurityGroups[0].GroupId" --output text $ACCOUNT_PROFILE 2>/dev/null)
+if [ -z "$SGID" ]; then
+ echo "Provisioning ssh-access security group"
+ SGID=$(aws ec2 create-security-group --description "SSH access for all EC2 instances" --group-name "ssh-access" --query "GroupId" --output text $ACCOUNT_PROFILE)
+
+ if [ -z "$SGID" ]; then
+ echo "Failed to provision ssh-access security group"
+ fi
+else
+ echo "ssh-access security group already exists '$SGID'"
+fi
+
# Get developers SSH public key from Parameter Store /SSH-Key/ and store as ~/.ssh/developers.pub
echo "Getting developers SSH public key"
mkdir ~/.ssh &>/dev/null
@@ -209,7 +230,9 @@ else
fi
# Update SSH Whitelist for this account
-"${awsDir}refresh_ssh_whitelist.sh" "" "" $ACCOUNT_ID
+if [ -n "$SGID" ]; then
+ "${awsDir}refresh_ssh_whitelist.sh" "" "" $ACCOUNT_ID
+fi
# Provision EFS unless set to false
if [ "$PROVISION_EFS" != 'false' ]; then
@@ -234,8 +257,7 @@ if [ "$PROVISION_EFS" != 'false' ]; then
echo "EFS stack already exists"
else
- # Find Default VPC and security groups
- VPCID=$(aws ec2 describe-vpcs --filters Name=is-default,Values=true --query "Vpcs[0].VpcId" --output text $ACCOUNT_PROFILE 2>/dev/null)
+ # Find Default VPC security group and IP CIDRs
VPCIP4CIDR=$(aws ec2 describe-vpcs --filters Name=is-default,Values=true --query "Vpcs[0].CidrBlockAssociationSet[0].CidrBlock" --output text $ACCOUNT_PROFILE 2>/dev/null)
VPCIP6CIDR=$(aws ec2 describe-vpcs --filters Name=is-default,Values=true --query "Vpcs[0].Ipv6CidrBlockAssociationSet[0].Ipv6CidrBlock" --output text $ACCOUNT_PROFILE 2>/dev/null)
@@ -247,6 +269,7 @@ if [ "$PROVISION_EFS" != 'false' ]; then
if [ -z "$SGID" ]; then
echo "Security group for default VPC not found"
+ exit 1
else
SUBNETIDS=$(aws ec2 describe-subnets --filters Name=vpc-id,Values=$VPCID --query "Subnets[*].SubnetId" --output text $ACCOUNT_PROFILE 2>/dev/null)