lunes, 27 de octubre de 2025

Option 1: Import existing resources into SAM Template

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. 

 The steps should be as follow:

  • Grab the code of the stack.
  • Create a file with the CloudFormation code that you already have, remove the transform tag. And add the code of what you want to import, in this case we are importing a DynamoDB table.
    AWSTemplateFormatVersion: '2010-09-09'
    Description: SAM Template with DynamoDB Table and SNS Topic

    Resources:
    MySNSTopic:
    Type: AWS::SNS::Topic
    Properties:
    TopicName: MyNotificationTopic
    Metadata:
    SamResourceId: MySNSTopic

    SecondaryTable:
    Type: AWS::DynamoDB::Table
    DeletionPolicy: Retain
    Properties:
    TableName: SecondaryTable
    BillingMode: PAY_PER_REQUEST
    AttributeDefinitions:
    - AttributeName: id
    AttributeType: S
    KeySchema:
    - AttributeName: id
    KeyType: HASH

  • Go to the stack actions -> Import resources into the stack.
  • Choose your CF template. 
  • Enter the name of the dynamodb table.
  • You will see a brief of the import.
  • The import will be completed.
  • Then update your SAM Template.
    AWSTemplateFormatVersion: '2010-09-09'
    Transform: AWS::Serverless-2016-10-31
    Description: SAM Template with DynamoDB Table and SNS Topic

    Resources:
    MySNSTopic:
    Type: AWS::SNS::Topic
    Properties:
    TopicName: MyNotificationTopic

    SecondaryTable:
    Type: AWS::DynamoDB::Table
    DeletionPolicy: Retain
    Properties:
    TableName: SecondaryTable
    BillingMode: PAY_PER_REQUEST
    AttributeDefinitions:
    - AttributeName: id
    AttributeType: S
    KeySchema:
    - AttributeName: id
    KeyType: HASH
  • Run sam build and sam deploy.
  • It will be managed by SAM now.

No hay comentarios:

Publicar un comentario