lunes, 27 de octubre de 2025

Option 2: Import existing resources into SAM Template using IaC Generator

Let's imagine you have a SAM template to your App, and for some reason you created manually resources. And you want to manage those resources created already into your template. 

  • Go to CloudFormation -> IaC Generator. Click Scan specific resources.
     
  •  Search for DynamoDB. Click on Scan.
     
  • After the scan is finished. Click Create template.
  • Click Update the template for an existing stack, choose the SAM stack.
  • Then enter a template name, deletion policy and update policy. Click on next.
  • Choose the table that you want to import. Click on next and Next again.
  • Then you can review and click Create template.
  • Review the template and Click on Import to stack.
  • Review and click Next.
  • Review and click Import resources.
  • The resource will be imported to the SAM template.

Now you will have the CloudFormation template updated, but let's update our local code to continue developing.

# Get the template and process it to extract the YAML content
aws cloudformation get-template --stack-name test --output json | jq -r '.TemplateBody' > template-updated.yaml

 

Update your code with Transform

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: SAM Template with DynamoDB Table and SNS Topic

Resources:
SecondaryTable:
Metadata:
SamResourceId: "SecondaryTable"
Type: "AWS::DynamoDB::Table"
DeletionPolicy: "Retain"
Properties:
BillingMode: "PAY_PER_REQUEST"
TableName: "SecondaryTable"
AttributeDefinitions:
- AttributeName: "id"
AttributeType: "S"
KeySchema:
- KeyType: "HASH"
AttributeName: "id"
DynamoDBTablePrimaryTable:
UpdateReplacePolicy: "Retain"
Type: "AWS::DynamoDB::Table"
DeletionPolicy: "Retain"
Properties:
SSESpecification:
SSEEnabled: false
TableName: "PrimaryTable"
AttributeDefinitions:
- AttributeType: "S"
AttributeName: "id"
ContributorInsightsSpecification:
Enabled: false
BillingMode: "PAY_PER_REQUEST"
PointInTimeRecoverySpecification:
PointInTimeRecoveryEnabled: false
WarmThroughput:
ReadUnitsPerSecond: 12000
WriteUnitsPerSecond: 4000
KeySchema:
- KeyType: "HASH"
AttributeName: "id"
DeletionProtectionEnabled: false
TableClass: "STANDARD"
Tags: []
TimeToLiveSpecification:
Enabled: false
MySNSTopic:
Properties:
TopicName: "MyNotificationTopic"
Metadata:
SamResourceId: "MySNSTopic"
Type: "AWS::SNS::Topic"


Run sam build and sam deploy.

No hay comentarios:

Publicar un comentario