arabold / serverless-export-env

Serverless plugin to export environment variables into a .env file
MIT License
103 stars 34 forks source link

Fn::GetAtt seems to not working #41

Open ademoverflow opened 2 years ago

ademoverflow commented 2 years ago

Hello !

I am facing an issue where I create a Cloudfront distribution and I cannot get the DomainName of the resource with GetAtt.

Here is my serverless.yaml:

service: my-service

useDotenv: true

package:
  individually: true

plugins:
  - serverless-export-env

provider:
  name: aws
  region: ${opt:region, "eu-west-1"}
  stage: ${opt:stage, "staging"}
  environment:
    CLOUDFRONT_WEBAPP_ID: !Ref WebAppCloudFrontDistribution
    # THE LINE BELLOW CAUSES THE ERROR AT THE END OF THE ISSUE:
    CLOUDFRONT_WEBAPP_URL:
      Fn::GetAtt:
        - WebAppCloudFrontDistribution
        - DomainName

custom:
  bucketName: my-awesome-bucket-789456
  export-env:
    filename: .env.${self:provider.stage}
    overwrite: true

resources:
  Resources:
    WebAppS3Bucket:
      Type: AWS::S3::Bucket
      Properties:
        BucketName: ${self:custom.bucketName}
        AccessControl: PublicRead
        WebsiteConfiguration:
          IndexDocument: index.html
          ErrorDocument: 404.html
    WebAppS3BucketPolicy:
      Type: AWS::S3::BucketPolicy
      Properties:
        Bucket:
          Ref: WebAppS3Bucket
        PolicyDocument:
          Statement:
            - Sid: PublicReadGetObject
              Effect: Allow
              Principal: "*"
              Action:
                - s3:GetObject
              Resource:
                "Fn::Join":
                  - ""
                  - - "arn:aws:s3:::"
                    - Ref: WebAppS3Bucket
                    - /*
    WebAppCloudFrontDistribution:
      Type: AWS::CloudFront::Distribution
      Properties:
        DistributionConfig:
          Origins:
            - DomainName:
                !Select [2, !Split ["/", !GetAtt WebAppS3Bucket.WebsiteURL]]
              Id: S3-Website-example.com.s3-website-${self:provider.region}.amazonaws.com
              CustomOriginConfig:
                HTTPPort: 80
                HTTPSPort: 443
                OriginProtocolPolicy: http-only
          Enabled: true
          DefaultCacheBehavior:
            AllowedMethods:
              - DELETE
              - GET
              - HEAD
              - OPTIONS
              - PATCH
              - POST
              - PUT
            TargetOriginId: S3-Website-example.com.s3-website-${self:provider.region}.amazonaws.com
            ForwardedValues:
              QueryString: false
              Cookies:
                Forward: none
            ViewerProtocolPolicy: redirect-to-https

package.json

{
  "name": "my-service",
  "private": true,
  "version": "1.0.0",
  "dependencies": {},
  "devDependencies": {
    "serverless-export-env": "^2.0.0"
  }
}

Firt I run: sls deploy --stage dev --region eu-west-1 and everything is ok, my stack my-service-dev contains the resources.

Then, when I run sls export-env --stage dev --region eu-west-1 I get the following result:

Fn::GetAttResolvers not found in params file: WebAppCloudFrontDistribution.DomainName

So I cannot fetch the cloudfront domain name and export it ...

Note: Ref works like a charm ! I can fetch the cloudfront ID without any error.

What am I doing wrong ?

ademoverflow commented 2 years ago

Oh .... https://github.com/arabold/serverless-export-env/blob/v2.0.0/src/lib/resolveCloudFormationEnvVariables.js

This is clearly not working, but the README suggests that the feature is enabled.

zezke commented 2 years ago

I am running into the same issue with the serverless-appsync-plugin for exposing the GraphQLApiUrl.

custom:
  appName: my-app
  appSync: ${file(serverless.appsync.yml)}
  serverlessTerminationProtection:
    stages: [ int, dev, qa, prod ]
  cognitoUserPoolId: ${cf:${self:custom.infraStack}.CognitoUserPoolId}
  graphQlApiUrl: !GetAtt GraphQlApi.GraphQLUrl

Did you manage to find a workaround?

arabold commented 2 years ago

Unfortunately Fn::GetAtt isn't working out of the box. You will have to define a manual mapping as described in the Custom Resource Resolution section in the README.md. I should have made that clearer though :(

eduardo3g commented 2 years ago

Unfortunately this manual mapping breaks integration and end-to-end tests. Is there any plan to cover all the scenarios of intrinsic functions? Thanks in advance!

pokryfka commented 2 years ago

Unfortunately Fn::GetAtt isn't working out of the box. You will have to define a manual mapping as described in the Custom Resource Resolution section in the README.md. I should have made that clearer though :(

!GetAtt GraphQlApi.GraphQLUrl used to be resolved properly for me with serverless v2

just updated to serverless v3 and it does not work anymore, get errors:

Fn::GetAttResolvers not found in params file: devGraphQlApi.GraphQLUrl
Fn::GetAttResolvers not found in params file: RedisReplicationGroup.PrimaryEndPoint.Address

I am not sure how much its the same problem?

Does the plugin supports serverless v3?