@@ -112,101 +112,40 @@ echo "This may take 15-30 minutes to complete."
112112
113113# SECURITY IMPROVED: Use least privilege access policy
114114# This policy restricts access to specific actions and should be further restricted to specific principals in production
115- ACCESS_POLICY=" {\" Version\" :\" 2012-10-17\" ,\" Statement\" :[{\" Effect\" :\" Allow\" ,\" Principal\" :{\" AWS\" :\" arn:aws:iam::${ACCOUNT_ID} :root\" },\" Action\" :[\" es:ESHttpGet\" ,\" es:ESHttpPut\" ,\" es:ESHttpPost\" ,\" es:ESHttpDelete\" ,\" es:ESHttpHead\" ],\" Resource\" :\" arn:aws:es:${AWS_REGION} :${ACCOUNT_ID} :domain/${DOMAIN_NAME} /*\" }]}"
116-
117- echo " Access policy created for region: $AWS_REGION "
118- echo " Access policy: [REDACTED]"
119-
120- # Create the domain (matches tutorial command exactly)
121- echo " Creating domain $DOMAIN_NAME ..."
122- CREATE_OUTPUT=$( aws opensearch create-domain \
123- --domain-name " $DOMAIN_NAME " \
124- --engine-version " OpenSearch_2.11" \
125- --cluster-config " InstanceType=t3.small.search,InstanceCount=1,ZoneAwarenessEnabled=false" \
126- --ebs-options " EBSEnabled=true,VolumeType=gp3,VolumeSize=10" \
127- --node-to-node-encryption-options " Enabled=true" \
128- --encryption-at-rest-options " Enabled=true" \
129- --domain-endpoint-options " EnforceHTTPS=true,TLSSecurityPolicy=Policy-Min-TLS-1-2-2019-07" \
130- --advanced-security-options " Enabled=true,InternalUserDatabaseEnabled=true,MasterUserOptions={MasterUserName=$MASTER_USER ,MasterUserPassword=$MASTER_PASSWORD }" \
131- --access-policies " $ACCESS_POLICY " \
132- --tags " Key=Environment,Value=Tutorial" " Key=Purpose,Value=OpenSearchGettingStarted" " Key=project,Value=doc-smith" " Key=tutorial,Value=opensearch-service-gs" 2>&1 )
133-
134- # Check if domain creation was successful
135- if [[ $? -ne 0 ]]; then
136- echo " Failed to create OpenSearch domain:"
137- echo " $CREATE_OUTPUT "
138- handle_error " Domain creation failed"
139- fi
140-
141- # Verify the domain was actually created by checking the output
142- if echo " $CREATE_OUTPUT " | grep -q " DomainStatus" ; then
143- echo " Domain creation initiated successfully."
144- DOMAIN_CREATED=true
145- else
146- echo " Domain creation output:"
147- echo " $CREATE_OUTPUT "
148- handle_error " Domain creation may have failed - no DomainStatus in response"
149- fi
150-
151- # Wait for domain to become active (improved logic)
152- echo " Waiting for domain to become active..."
153- RETRY_COUNT=0
154- MAX_RETRIES=45 # 45 minutes with 60 second intervals
155-
156- while [[ $RETRY_COUNT -lt $MAX_RETRIES ]]; do
157- echo " Checking domain status... (attempt $(( RETRY_COUNT+ 1 )) /$MAX_RETRIES )"
158-
159- # Get domain status
160- DOMAIN_STATUS=$( aws opensearch describe-domain --domain-name " $DOMAIN_NAME " 2>&1 )
161-
162- if [[ $? -ne 0 ]]; then
163- echo " Error checking domain status:"
164- echo " $DOMAIN_STATUS "
165-
166- # If domain not found after several attempts, it likely failed to create
167- if [[ $RETRY_COUNT -gt 5 ]] && echo " $DOMAIN_STATUS " | grep -q " ResourceNotFoundException" ; then
168- handle_error " Domain not found after multiple attempts. Domain creation likely failed."
169- fi
170-
171- echo " Will retry in 60 seconds..."
172- else
173- # Check if domain is no longer processing
174- if echo " $DOMAIN_STATUS " | grep -q ' "Processing": false' ; then
175- DOMAIN_ACTIVE=true
176- echo " Domain is now active!"
177- break
178- else
179- echo " Domain is still being created. Checking again in 60 seconds..."
180- fi
181- fi
182-
183- sleep 60
184- RETRY_COUNT=$(( RETRY_COUNT+ 1 ))
185- done
186-
187- # Verify domain is active
188- if [[ " $DOMAIN_ACTIVE " != " true" ]]; then
189- echo " Domain creation is taking longer than expected ($(( MAX_RETRIES)) minutes)."
190- echo " You can check the status later using:"
191- echo " aws opensearch describe-domain --domain-name $DOMAIN_NAME "
192- handle_error " Domain did not become active within the expected time"
193- fi
194-
195- # Get domain endpoint (matches tutorial)
196- echo " Retrieving domain endpoint..."
197- DOMAIN_ENDPOINT=$( aws opensearch describe-domain --domain-name " $DOMAIN_NAME " --query ' DomainStatus.Endpoint' --output text)
198-
199- if [[ $? -ne 0 ]] || [[ -z " $DOMAIN_ENDPOINT " ]] || [[ " $DOMAIN_ENDPOINT " == " None" ]]; then
200- handle_error " Failed to get domain endpoint"
115+ # Create the domain using CloudFormation (handles the 15-20 min creation wait)
116+ SCRIPT_DIR=" $( cd " $( dirname " ${BASH_SOURCE[0]} " ) " && pwd) "
117+ STACK_NAME=" tutorial-opensearch-${RANDOM_ID} "
118+
119+ echo " Creating domain $DOMAIN_NAME via CloudFormation..."
120+ echo " This typically takes 15-20 minutes. CloudFormation will wait for completion."
121+ aws cloudformation deploy \
122+ --template-file " $SCRIPT_DIR /cfn-opensearch-domain.yaml" \
123+ --stack-name " $STACK_NAME " \
124+ --parameter-overrides \
125+ DomainName=" $DOMAIN_NAME " \
126+ MasterUserName=" $MASTER_USER " \
127+ MasterUserPassword=" $MASTER_PASSWORD " \
128+ --tags project=doc-smith tutorial=opensearch-service-gs \
129+ --no-fail-on-empty-changeset 2>&1 || handle_error " CloudFormation stack creation failed"
130+
131+ DOMAIN_CREATED=true
132+ echo " Domain created successfully via CloudFormation."
133+
134+ # Get domain endpoint from stack outputs
135+ DOMAIN_ENDPOINT=$( aws cloudformation describe-stacks \
136+ --stack-name " $STACK_NAME " \
137+ --query ' Stacks[0].Outputs[?OutputKey==`DomainEndpoint`].OutputValue' \
138+ --output text)
139+
140+ if [[ -z " $DOMAIN_ENDPOINT " ]] || [[ " $DOMAIN_ENDPOINT " == " None" ]]; then
141+ handle_error " Failed to get domain endpoint from CloudFormation outputs"
201142fi
202143
203144echo " Domain endpoint: $DOMAIN_ENDPOINT "
204145
205- # Wait additional time for fine-grained access control to be fully ready
206- echo " Domain is active, but waiting additional time for fine-grained access control to be fully ready..."
207- echo " Fine-grained access control can take several minutes to initialize after domain becomes active."
208- echo " Waiting 8 minutes for full initialization..."
209- sleep 480 # Wait 8 minutes for fine-grained access control to be ready
146+ # Wait for fine-grained access control to be fully ready
147+ echo " Waiting for fine-grained access control to initialize..."
148+ sleep 120
210149
211150# Verify variables are set correctly (matches tutorial)
212151echo " Verifying configuration..."
@@ -534,21 +473,22 @@ CLEANUP_CHOICE="y"
534473
535474if [[ " ${CLEANUP_CHOICE,,} " == " y" ]]; then
536475 echo " Cleaning up resources..."
537- aws opensearch delete-domain --domain-name " $DOMAIN_NAME "
538- echo " ✓ Cleanup initiated. Domain deletion may take several minutes to complete."
539- echo " "
540- echo " You can check the deletion status using:"
541- echo " aws opensearch describe-domain --domain-name $DOMAIN_NAME "
542- echo " "
543- echo " When deletion is complete, you'll see a 'Domain not found' error."
476+ if [ -n " ${STACK_NAME:- } " ]; then
477+ echo " Deleting CloudFormation stack $STACK_NAME ..."
478+ aws cloudformation delete-stack --stack-name " $STACK_NAME "
479+ echo " ✓ Stack deletion initiated. This may take several minutes."
480+ echo " Monitor: aws cloudformation describe-stacks --stack-name $STACK_NAME "
481+ else
482+ aws opensearch delete-domain --domain-name " $DOMAIN_NAME " 2> /dev/null
483+ echo " ✓ Domain deletion initiated."
484+ fi
544485else
545486 echo " Resources will NOT be deleted automatically."
546487 echo " "
547- echo " To delete the domain later, use :"
548- echo " aws opensearch delete-domain --domain -name $DOMAIN_NAME "
488+ echo " To delete later:"
489+ echo " aws cloudformation delete-stack --stack -name ${STACK_NAME :- tutorial-opensearch-XXXX} "
549490 echo " "
550491 echo " ⚠ IMPORTANT: Keeping these resources will incur ongoing AWS charges!"
551- echo " Estimated cost: ~$0 .038/hour (~$0 .91/day)"
552492fi
553493
554494# Clean up temporary files (handled by trap)
0 commit comments