पाठ 19 / 32
Infrastructure as Code for Serverless
यह पाठ अभी हिंदी में अनुवादित नहीं है, इसलिए अंग्रेज़ी में दिखाया जा रहा है।
Describe functions, triggers, permissions and tables in a reviewable template and deploy them repeatably.
लेखक: Rahul Agarwal · 15 साल से वेब डेवलपर
Many small resources, one source of truth
A serverless app is many small cloud resources: functions, permissions, routes, queues, tables and schedules. Clicking them together in a web console is not reproducible, and nobody can review it. Infrastructure as code (IaC) describes them in files that live in Git, get reviewed in pull requests and deploy the same way every time. Options range from general tools (Terraform or OpenTofu, AWS CDK, Pulumi) to serverless-focused ones: AWS SAM adds concise resource types on top of CloudFormation, the Serverless Framework and SST offer higher-level abstractions, and Wrangler configures and deploys Cloudflare Workers. Whatever you choose, the template should declare each function's runtime, memory, timeout, environment variables, triggers and a least-privilege role. Then the permission to read one table is reviewed in the same pull request as the code that reads it, and staging and production are built from the same definition.
An AWS SAM template
One function with an HTTP route, a table, and a policy template that grants read-only access to that table alone. !Ref wires resources together, so no names are hard-coded.
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Resources:
WeatherFunction:
Type: AWS::Serverless::Function
Properties:
Runtime: nodejs22.x
Handler: index.handler
CodeUri: src/
MemorySize: 512
Timeout: 10
Environment:
Variables:
TABLE_NAME: !Ref ReadingsTable
Policies:
- DynamoDBReadPolicy:
TableName: !Ref ReadingsTable
Events:
GetWeather:
Type: HttpApi
Properties:
Path: /weather
Method: get
ReadingsTable:
Type: AWS::Serverless::SimpleTableFrom commit to a live version
Code is committed, built into a deployable package, deployed by the IaC tool, and published as a new version that traffic can be shifted to.
Runtimes have an end-of-life date
Managed runtimes such as nodejs22.x are deprecated some time after the language version reaches end of life, after which you can no longer create or update functions on them. Keep runtime versions in the template, track the provider's deprecation schedule, and upgrade like any other dependency.
त्वरित जाँच: What is the main benefit of declaring a function's IAM permissions in the same IaC template as the function?
- Permissions become unnecessary once they are in a template
- The function runs faster
- Permission changes are versioned and reviewed together with the code that needs them
- The template automatically grants administrator access during development
Answer
Permission changes are versioned and reviewed together with the code that needs them — IaC puts permissions under version control and code review, so least privilege can actually be checked. It does not change performance or remove the need for permissions.