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.
   aliyun ecs AuthorizeSecurityGroup --SecurityGroupId sg-xxx --IpProtocol tcp --PortRange 3306/3306 --SourceCidrIp 10.0.0.0/24
  1. Provision RDS Instance: Create the database in the same VPC/VSwitch as the ECS target.
   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
  1. Create Application Account: Initialize a dedicated user (routes to rds-manage-accounts).
   aliyun rds CreateAccount --DBInstanceId rm-xxx --AccountName app_user --AccountPassword 'SecurePass123!' --AccountType Normal
  1. Grant Privileges: Assign read/write access to the target schema.
   aliyun rds GrantAccountPrivilege --DBInstanceId rm-xxx --AccountName app_user --DBName app_db --AccountPrivilege ReadWrite
  1. Launch ECS Instance: Attach to the preconfigured security group and VSwitch.
   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
  1. Inject Credentials: Export environment variables in your deployment script or CI/CD pipeline.
   export DB_HOST=rm-xxx.mysql.rds.aliyuncs.com
   export DB_PORT=3306
   export DB_USER=app_user
   export DB_PASS='SecurePass123!'
  1. Validate Connectivity: Run a quick TCP and auth test from the ECS instance.
   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

FAQ

Q: How do I provision and deploy a web application backend with a database? A: You must provision an ECS compute instance and an ApsaraDB RDS instance within the same VPC and securely connect them via private networking and least-privilege accounts. The integration workflow involves configuring security groups for MySQL traffic, initializing a dedicated RDS user with explicit schema privileges, launching the compute instance, injecting credentials as environment variables, and validating connectivity.