DaaS / Products / Deploy Web App Backend with Database

Deploy Web App Backend with Database

A developer provisions an ECS instance with proper security group rules to allow database connectivity, creates an RDS instance for the application backend, and sets up database accounts with appropriate permissions so the application can securely connect to the database.

Products involved

Scenario

When deploying a web application backend that requires persistent relational storage, developers must provision an ECS compute instance alongside an ApsaraDB RDS instance, then securely bridge them via VPC networking and least-privilege database accounts. This workflow ensures the application can authenticate and query the database without exposing credentials or ports to the public internet.

Integration steps

  1. Configure ECS Security Group: Allow inbound MySQL traffic from your private subnet.
  2. ``bash aliyun ecs AuthorizeSecurityGroup --SecurityGroupId sg-xxx --IpProtocol tcp --PortRange 3306/3306 --SourceCidrIp 10.0.0.0/24 ``

  3. Provision RDS Instance: Create the database in the same VPC/VSwitch as the ECS target.
  4. ``bash aliyun rds CreateDBInstance --Engine MySQL --EngineVersion 8.0 --DBInstanceClass rds.mysql.s2.large --VPCId vpc-xxx --VSwitchId vsw-xxx --SecurityIPList 10.0.0.0/24 ``

  5. Create Application Account: Initialize a dedicated user (routes to rds-manage-accounts).
  6. ``bash aliyun rds CreateAccount --DBInstanceId rm-xxx --AccountName app_user --AccountPassword 'SecurePass123!' --AccountType Normal ``

  7. Grant Privileges: Assign read/write access to the target schema.
  8. ``bash aliyun rds GrantAccountPrivilege --DBInstanceId rm-xxx --AccountName app_user --DBName app_db --AccountPrivilege ReadWrite ``

  9. Launch ECS Instance: Attach to the preconfigured security group and VSwitch.
  10. ``bash aliyun ecs RunInstances --InstanceType ecs.t6-c1m2.large --ImageId aliyun_3_x64_20G_alibase_20230920.vhd --VSwitchId vsw-xxx --SecurityGroupId sg-xxx --InstanceName web-backend ``

  11. Inject Credentials: Export environment variables in your deployment script or CI/CD pipeline.
  12. ``bash export DB_HOST=rm-xxx.mysql.rds.aliyuncs.com export DB_PORT=3306 export DB_USER=app_user export DB_PASS='SecurePass123!' ``

  13. Validate Connectivity: Run a quick TCP and auth test from the ECS instance.
  14. ``bash nc -zv $DB_HOST $DB_PORT && mysql -h $DB_HOST -P $DB_PORT -u $DB_USER -p$DB_PASS -e "SELECT 1;" ``

Architecture

The ECS instance runs the application runtime and acts as the database client. All traffic flows over the private VPC network to the RDS endpoint, which handles query execution, connection pooling, and automated backups. Security groups enforce network-layer isolation, while RDS account privileges enforce application-layer data access control.

Prerequisites

Common pitfalls

Typical questions