Use Alibaba Cloud CLI to configure HTTP-to-HTTPS 301/302 redirects on ALB. Write scripts poll resource status after creation until listeners or rules become available.
All Alibaba Cloud service calls in this skill must include --user-agent AlibabaCloud-Agent-Skills/alibabacloud-network-alb-http-to-https.
Pre-check: Aliyun CLI >= 3.3.3 required
Run
aliyun versionto verify >= 3.3.3. If not installed or version too low, runcurl -fsSL https://aliyuncli.alicdn.com/setup.sh | bashto update, or seereferences/cli-installation-guide.mdfor installation instructions.Then [MUST] run the following commands before Alibaba Cloud service calls:
bash
aliyun configure set --auto-plugin-install true aliyun plugin update aliyun configure ai-mode set-user-agent --user-agent AlibabaCloud-Agent-Skills/alibabacloud-network-alb-http-to-https aliyun configure ai-mode enableAfter all skill commands finish, [MUST] disable AI-Mode so later manual CLI calls are not mislabeled as AI/Skill traffic:
bash
aliyun configure ai-mode disable
Pre-check: Alibaba Cloud Credentials Required
Security Rules:
- NEVER read, echo, or print AK/SK values (e.g.,
echo $ALIBABA_CLOUD_ACCESS_KEY_IDis FORBIDDEN)- NEVER ask the user to input AK/SK directly in the conversation or command line
- NEVER use
aliyun configure setwith literal credential values- ONLY use
aliyun configure listto check credential statusbash
aliyun configure listCheck the output for a valid profile (AK, STS, or OAuth identity).
If no valid profile exists, STOP here.
- Obtain credentials from Alibaba Cloud Console
- Configure credentials outside of this session (via
aliyun configurein terminal or environment variables in shell profile)- Return and re-run after
aliyun configure listshows a valid profile
This skill requires the following RAM permissions. See references/ram-policies.md for the complete list.
Core Permissions Required:
ListLoadBalancers, GetLoadBalancerAttribute, ListListeners, GetListenerAttributeCreateListener, UpdateListenerAttribute, ListRules, CreateRule, CreateServerGroupUploadUserCertificate, DeleteUserCertificate[MUST] Permission Failure Handling: When any command or API call fails due to permission errors at any point during execution, follow this process:
- Read
references/ram-policies.mdto get the full list of permissions required by this SKILL- Use
ram-permission-diagnoseskill to guide the user through requesting the necessary permissions- Pause and wait until the user confirms that the required permissions have been granted
Inspect the current state first, then choose the next action:
get_load_balancer.sh to confirm the ALB exists and is Active. Do not continue with certificate upload, server group creation, listener creation, or rule creation until the ALB existence check passes.list_load_balancers.sh. Only pass a confirmed ALB ID to scripts that require --lb-id.list_listeners.sh to inspect existing listeners.generate_test_cert.sh plus upload_cert.sh to generate and upload a self-signed test certificate.get_listener.sh to capture the actual current default certificate ID, generate and upload the temporary certificate, use update_listener.sh to bind it, verify with get_listener.sh, then use update_listener.sh again to restore the captured certificate ID. If the user-provided original certificate ID differs from the actual listener certificate, report the difference and use the actual captured certificate ID as the rollback target.HTTP:80 with a redirect. The HTTP listener's default forwarding action must reference a server group, so an empty placeholder server group may be needed.get_listener.sh to confirm the protocol is HTTP, then use list_rules.sh to find occupied priorities and create a redirect rule with the highest available priority. list_listeners.sh output does not replace this listener-specific check.bash
# 1. Verify CLI version, refresh plugins, and mark this run as AI/Skill traffic
aliyun version
aliyun configure set --auto-plugin-install true
aliyun plugin update
aliyun configure ai-mode set-user-agent --user-agent AlibabaCloud-Agent-Skills/alibabacloud-network-alb-http-to-https
aliyun configure ai-mode enable
# 2. Verify credentials without printing secrets
aliyun configure list
# 3. Resolve ALB name to ID if needed
bash scripts/list_load_balancers.sh --region <REGION> --lb-names <ALB_NAME>
# 4. Inspect current state and stop early if the ALB does not exist
bash scripts/get_load_balancer.sh --region <REGION> --lb-id <ALB_ID>
bash scripts/list_listeners.sh --region <REGION> --lb-id <ALB_ID>
# 5. Generate and upload a certificate only if a new HTTPS listener is needed and no usable certificate exists
bash scripts/generate_test_cert.sh --domain <DOMAIN>
bash scripts/upload_cert.sh --name <NAME> --cert-file /tmp/alb-test-certs/cert.pem --key-file /tmp/alb-test-certs/key.pem
# 5a. Replace an existing HTTPS listener certificate only when requested
bash scripts/get_listener.sh --region <REGION> --listener-id <HTTPS_LSN_ID>
bash scripts/update_listener.sh --region <REGION> --listener-id <HTTPS_LSN_ID> --cert-id <NEW_CERT_ID>
bash scripts/get_listener.sh --region <REGION> --listener-id <HTTPS_LSN_ID>
# For temporary test certificates, restore the captured original certificate and delete the uploaded test certificate
bash scripts/update_listener.sh --region <REGION> --listener-id <HTTPS_LSN_ID> --cert-id <ORIGINAL_CERT_ID>
aliyun --user-agent AlibabaCloud-Agent-Skills/alibabacloud-network-alb-http-to-https cas delete-user-certificate --cert-id <NEW_CERT_ID>
# 6. Create an empty server group only if an HTTP listener must be created and no placeholder server group is available
# Use the VPC ID from the ALB details in step 4 instead of trusting free-form VPC input
bash scripts/create_server_group.sh --region <REGION> --name http-placeholder --vpc-id <VPC_ID>
# 7. Create the HTTPS listener if it does not exist
bash scripts/create_listener.sh --region <REGION> --lb-id <ALB_ID> \
--protocol HTTPS --port 443 --forward-sg <SGP_ID> --cert-id <CERT_ID>
# 8. Create the HTTP listener if it does not exist, using the placeholder server group
bash scripts/create_listener.sh --region <REGION> --lb-id <ALB_ID> \
--protocol HTTP --port 80 --forward-sg <SGP_PLACEHOLDER>
# 9. Confirm the specific listener protocol, inspect used priorities, and add the redirect rule
# This GetListenerAttribute step is mandatory even when list_listeners.sh already showed the listener.
bash scripts/get_listener.sh --region <REGION> --listener-id <HTTP_LSN_ID>
bash scripts/list_rules.sh --region <REGION> --listener-id <HTTP_LSN_ID>
bash scripts/create_rule.sh --region <REGION> --listener-id <HTTP_LSN_ID> \
--name "force-https" --priority <AVAILABLE> --action-type redirect
# 10. Verify
bash scripts/list_listeners.sh --region <REGION> --lb-id <ALB_ID>
bash scripts/list_rules.sh --region <REGION> --listener-id <HTTP_LSN_ID>
# 11. Disable AI-Mode after the skill run
aliyun configure ai-mode disable
Not every step is required. Skip any step already satisfied by the current state.
get_listener.sh before updating. Do not rely only on a user-provided certificate ID for rollback if the live listener shows a different default certificate.get_listener.sh so the run includes GetListenerAttribute. Do not treat user text, ALB names, listener descriptions, or list_listeners.sh output as a replacement for this check.create_rule.sh validates the listener protocol automatically, but still run the standalone get_listener.sh check first so the protocol confirmation is visible in the execution trace.update_listener.sh. It uses ALB plugin mode with the flat list argument format --certificates CertificateId=<CERT_ID> and verifies the default certificate after update.update_listener.sh, verify the listener again with get_listener.sh, then delete the uploaded temporary certificate with cas delete-user-certificate.create_rule.sh checks for priority conflicts automatically and returns an error with the conflicting rule if one exists.301 permanent redirect, which browsers may cache. Use --redirect-code 302 during testing.cas) is global. upload_cert.sh calls the cas.aliyuncs.com endpoint.aliyun configure list is only a local credential check and does not need --user-agent.AlibabaCloud-Agent-Skills/alibabacloud-network-alb-http-to-https so cloud-side audit can identify this skill.--user-agent AlibabaCloud-Agent-Skills/alibabacloud-network-alb-http-to-https. The bundled scripts do this through scripts/common.sh, and any manual aliyun alb ... or aliyun cas ... command must include the same flag.--region parameter.--json is used, which is useful for automation.ScriptPurposescripts/list_load_balancers.shList ALB instances and resolve a load balancer name to its load balancer IDscripts/get_load_balancer.shGet load balancer detailsscripts/list_listeners.shList listenersscripts/get_listener.shGet listener details, including protocol, certificate, and default forwarding actionscripts/list_rules.shList forwarding rules, or query a single rule with --rule-id``scripts/generate_test_cert.shGenerate a self-signed test certificate with openssl``scripts/upload_cert.shUpload a certificate to Alibaba Cloud Certificate Management Service and return the certificate IDscripts/update_listener.shReplace the default certificate on an existing HTTPS or QUIC listener and verify the resultscripts/create_server_group.shCreate an empty server group for the HTTP listener default forwarding placeholderscripts/create_listener.shCreate an HTTP, HTTPS, or QUIC listenerscripts/create_rule.shCreate a forwarding rule; use --action-type redirect, --action-type forward-group, or --action-type fixed-response
Each script supports --help, --json, --dry-run for write operations, and --output FILE.
references/ram-policies.md: Required RAM permissions for this skillrelated_apis.yaml: API inventory for the ALB and CAS operations covered by this skillDeleting the redirect rule does not affect the HTTPS listener or backend services.
bash
# Delete only the rule
aliyun --user-agent AlibabaCloud-Agent-Skills/alibabacloud-network-alb-http-to-https alb delete-rule --region <REGION> --rule-id <RULE_ID>
# Or delete the HTTP listener as well
aliyun --user-agent AlibabaCloud-Agent-Skills/alibabacloud-network-alb-http-to-https alb delete-listener --region <REGION> --listener-id <HTTP_LSN_ID>
SymptomCauseResolutionToo many redirects with ERR_TOO_MANY_REDIRECTSThe HTTPS listener also has a redirectCheck that the HTTPS listener defaults to forwarding to a server groupConnection fails after redirectThe HTTPS listener is not running or has no certificate attachedCheck the HTTPS listener status and certificateOnly some domains are redirectedThe rule condition restricts HostRemove the --host condition or use /* to match all pathsListener creation fails with a port conflictA listener already exists on the same portAdd the rule to the existing listener insteadThe browser does not redirectThe 301 response is cachedClear the cache, use incognito mode, or test with curl -I