100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps
Java asset-managementbuildingdashboarddataplatformdevice-managementedge-computingenergyenergy-managementenergy-monitorfleet-managementfleet-telematicsiotiot-applicationiot-platformmodbusmqttmqtt-brokerota-updaterules-enginesmartcity
Minor
Rich Turnerb65ded1d691a
diff --git a/.ci_cd/aws/provision_account.sh b/.ci_cd/aws/provision_account.shindex 9d8039395..a67b7f216 100644--- a/.ci_cd/aws/provision_account.sh+++ b/.ci_cd/aws/provision_account.sh@@ -27,7 +27,7 @@ elseawsDir=./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 existsource "${awsDir}get_account_id.sh"+CatalogName="account-$ACCOUNT_NAME"+if [ -n "$ACCOUNT_ID" ]; thenecho "Account already exists so skipping initial provisioning step"elseRandomToken=$(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" ]; thenecho "Failed to get product ID for Control Tower Account Factory you must be logged in to the management account"exit 1else@@ -63,14 +65,13 @@ elsepa_id=$(aws servicecatalog describe-product --id $prod_id --query "ProvisioningArtifacts[-1].Id" --output text)- if [ $? -ne 0 ]; then+ if [ $? -ne 0 ] || [ -z "$pa_id" ]; thenecho "Failed to get provisioning artifact ID for Control Tower Account Factory"exit 1elseecho "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" ]; thenROOT_ID=$(aws organizations list-roots --query "Roots[0].Id" --output text)@@ -92,7 +93,7 @@ elseSTATUS=$(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 ]; doecho "Found provisioned account status $STATUS .. Sleeping 30 seconds"sleep 30STATUS=$(aws servicecatalog scan-provisioned-products --query "ProvisionedProducts[?Name=='$CatalogName'].Status" --output text)@@ -106,13 +107,7 @@ else# Get account ID of newly provisioned accountProvisionedProductId=$(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)fiif [ -z "$ACCOUNT_ID" ]; then@@ -120,14 +115,14 @@ if [ -z "$ACCOUNT_ID" ]; thenexit 1fi-# 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 completeSTATUS=$(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 ]; doecho "Code pipeline for control tower customisations status is $STATUS .. Sleeping 30 seconds"sleep 30STATUS=$(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 ]; dodoneif [ "$STATUS" != 'Succeeded' ]; then- echo "Control tower customisation pipeline has failed"+ echo "Control tower customisation pipeline has failed cannot continue until this is resolved"exit 1elseecho "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.pubecho "Getting developers SSH public key"mkdir ~/.ssh &>/dev/null@@ -209,7 +230,9 @@ elsefi# 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 falseif [ "$PROVISION_EFS" != 'false' ]; then@@ -234,8 +257,7 @@ if [ "$PROVISION_EFS" != 'false' ]; thenecho "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 CIDRsVPCIP4CIDR=$(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' ]; thenif [ -z "$SGID" ]; thenecho "Security group for default VPC not found"+ exit 1elseSUBNETIDS=$(aws ec2 describe-subnets --filters Name=vpc-id,Values=$VPCID --query "Subnets[*].SubnetId" --output text $ACCOUNT_PROFILE 2>/dev/null)