Networking & Security

This section covers Steps 2–7 of the deployment: creating the VPC, subnets, internet gateway, NAT gateway, route tables, security groups, and IAM roles.


Step 2: Create VPC & Subnets

2.1 Create VPC

Console: VPC Dashboard → Your VPCsCreate VPC

FieldValue
Resources to createVPC and more
Name tagsmartinvoice-vpc
IPv4 CIDR10.0.0.0/16
TenancyDefault

alt text

2.2 Create 4 Subnets

Console: VPC → SubnetsCreate subnet (click Add new subnet to create all at once)

#NameAZCIDR
1smartinvoice-public-1aap-southeast-1a10.0.1.0/24
2smartinvoice-public-1bap-southeast-1b10.0.2.0/24
3smartinvoice-private-1aap-southeast-1a10.0.3.0/24
4smartinvoice-private-1bap-southeast-1b10.0.4.0/24

2.3 Enable Auto-assign Public IP

For each public subnet: Actions → Edit subnet settings → Enable auto-assign public IPv4


Step 3: Create Internet Gateway

Console: VPC → Internet GatewaysCreate internet gateway

Name tagsmartinvoice-igw

ActionsAttach to VPCsmartinvoice-vpcAttach


Step 4: Create NAT Gateway

NAT Gateway allows Private Subnets to access the Internet (outbound). Cost ~$32/month.

Console: VPC → NAT GatewaysCreate NAT gateway

FieldValue
Namesmartinvoice-nat-gw
Subnetsmartinvoice-public-1a (must be in a Public Subnet!)
ConnectivityPublic
Elastic IPClick Allocate Elastic IP

Wait for status Available (2–3 minutes).


Step 5: Create Route Tables

5.1 Public Route Table

Console: VPC → Route TablesCreate route table

NameVPC
smartinvoice-public-rtsmartinvoice-vpc

Routes → Edit → Add: 0.0.0.0/0 → Target: smartinvoice-igw

Subnet associations: tick smartinvoice-public-1a + smartinvoice-public-1b

5.2 Private Route Table

NameVPC
smartinvoice-private-rtsmartinvoice-vpc

Routes → Add: 0.0.0.0/0 → Target: smartinvoice-nat-gw

Subnet associations: tick smartinvoice-private-1a + smartinvoice-private-1b


Step 6: Create Security Groups

Console: VPC → Security GroupsCreate security group (VPC: smartinvoice-vpc)

SG 1: ALB (smartinvoice-alb-sg)

InboundPortSource
HTTP800.0.0.0/0
HTTPS4430.0.0.0/0

alt text

SG 2: Backend (smartinvoice-backend-sg)

InboundPortSource
HTTP80smartinvoice-alb-sg
Custom TCP8080smartinvoice-alb-sg

alt text

SG 3: RDS (smartinvoice-rds-sg)

InboundPortSource
PostgreSQL5432smartinvoice-backend-sg
PostgreSQL5432smartinvoice-ocr-sg

SG 4: OCR (smartinvoice-ocr-sg)

InboundPortSource
Custom TCP5000smartinvoice-backend-sg (Direct internal)

All SGs: Outbound = All traffic → 0.0.0.0/0


Step 7: Create IAM Roles

You need to create 4 IAM Roles. The general procedure for each:

General Steps:

  1. Go to IAM Console → Sidebar → RolesCreate role.
  2. Step 1: Trusted entity type: AWS service. Select the corresponding service. Click Next.
  3. Step 2: Search and tick the required policies. Click Next.
  4. Step 3: Enter the Role name exactly. Click Create role ✅.

7.1 EB EC2 Role (aws-elasticbeanstalk-ec2-role)

Grants EC2 instances in the Backend the right to access other AWS resources.

  • Trusted entity: AWS serviceElastic Beanstalk → Use case: Elastic Beanstalk - Compute
  • Policies:
    • AmazonS3FullAccess
    • AmazonSQSFullAccess
    • AmazonCognitoPowerUser
    • AmazonSSMReadOnlyAccess
    • AmazonEC2ContainerRegistryReadOnly
    • CloudWatchLogsFullAccess

alt text

7.2 EB Service Role (aws-elasticbeanstalk-service-role)

Allows Elastic Beanstalk to call AWS services on your behalf.

  • Trusted entity: AWS serviceElastic Beanstalk → Use case: Elastic Beanstalk - Environment
  • Policies (auto-attached by AWS):
    • AWSElasticBeanstalkEnhancedHealth
    • AWSElasticBeanstalkManagedUpdatesCustomerRolePolicy

alt text

7.3 ECS Execution Role (ecsTaskExecutionRole)

Allows ECS Fargate to pull images from ECR and write logs to CloudWatch.

  • Trusted entity: AWS serviceElastic Container ServiceElastic Container Service Task
  • Policies:
    • AmazonECSTaskExecutionRolePolicy
    • CloudWatchLogsFullAccess

7.4 ECS Task Role for OCR (smartinvoice-ecs-task-role)

Grants direct permissions to the OCR application running in the container.

  • Trusted entity: AWS serviceElastic Container ServiceElastic Container Service Task
  • Policies:
    • AmazonS3FullAccess
    • AmazonSQSFullAccess
    • AmazonSSMReadOnlyAccess