Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

!ImportValue in Serverless Framework not working

I'm attempting to export a DynamoDb StreamArn from a stack created in CloudFormation, then reference the export using !ImportValue in the serverless.yml.

But I'm getting this error message:

unknown tag !<!ImportValue> in "/codebuild/output/src/serverless.yml"

The cloudformation and serverless.yml are defined as below. Any help appreciated.

StackA.yml

AWSTemplateFormatVersion: 2010-09-09
Description: Resources for the registration site

Resources:
  ClientTable:
    Type: AWS::DynamoDB::Table
    DeletionPolicy: Retain
    Properties:
      TableName: client
      AttributeDefinitions:
        - AttributeName: id
          AttributeType: S
      KeySchema:
        - AttributeName: id
          KeyType: HASH
      ProvisionedThroughput:
        ReadCapacityUnits: 2
        WriteCapacityUnits: 2
      StreamSpecification:
        StreamViewType: NEW_AND_OLD_IMAGES

Outputs:  
  ClientTableStreamArn:
      Description: The ARN for My ClientTable Stream
      Value: !GetAtt ClientTable.StreamArn
      Export:
        Name: my-client-table-stream-arn

serverless.yml

service: my-service

frameworkVersion: ">=1.1.0 <2.0.0"

provider:
  name: aws
  runtime: nodejs6.10
  iamRoleStatements:
    - Effect: Allow
      Action:
        - dynamodb:DescribeStream
        - dynamodb:GetRecords
        - dynamodb:GetShardIterator
        - dynamodb:ListStreams
        - dynamodb:GetItem
        - dynamodb:PutItem
      Resource: arn:aws:dynamodb:*:*:table/client

functions:

  foo:
    handler: foo.main
    events:
      - stream:
          type: dynamodb
          arn: !ImportValue my-client-table-stream-arn
          batchSize: 1
like image 797
L G Avatar asked Sep 01 '17 15:09

L G


People also ask

What is FN ImportValue?

The intrinsic function Fn::ImportValue returns the value of an output exported by another stack. You typically use this function to create cross-stack references. In the following example template snippets, Stack A exports VPC security group values and Stack B imports them.

How do I refer to a property in serverless Yaml file?

To reference properties in other YAML files use the ${file(./myFile. yml):someProperty} syntax in your serverless. yml configuration file. To reference properties in other JSON files use the ${file(./myFile.

What is ref in serverless?

Ref. Ref is used to reference other resources or parameters in your template. Depending on the Resource it will return what data is typically needed the most from that resource. For example for a S3 Bucket it returns the bucket name, for an ACM Certificate it will return the ARN.


2 Answers

Solved by using ${cf:stackName.outputKey}

like image 118
L G Avatar answered Nov 11 '22 07:11

L G


I struggled with this as well, and what did trick for me was:

functions:
  foo:
    handler: foo.main
    events:
      - stream:
         type: dynamodb
         arn: 
          !ImportValue my-client-table-stream-arn
         batchSize: 1

Note, that intrinsic functions ImportValue is on a new line and indented, otherwise the whole event is ignored when cloudformation-template-update-stack.json is generated.

like image 31
Jenya Y. Avatar answered Nov 11 '22 09:11

Jenya Y.