I have a yaml cloud formation file which requires a variable stored in ssm parameter. The yaml file is a CFT template. Below is the sample code,
AWSTemplateFormatVersion: 2010-09-09
Description: 'Fully Automated OT Archival Data Migration'
Parameters:
Environment:
Description: 'Stage type (for Tags)'
Type: String
Default: dev
Resources:
S3Bucket:
Type: 'AWS::S3::Bucket'
Properties:
BucketName: '{{resolve:ssm:/opentext/config/automated-ot-archival-data-migration/migration.bucket.name:1}}-${Environment}'
When I upload the code to cloudformation in AWS console, I results with an error. I'm wondering whether the ssm param reference is correct or not.
Please let me know if you find any issues here.
Thanks
You are missing the !Sub function for your {Environment} variable.
BucketName: !Sub '{{resolve:ssm:/opentext/config/automated-ot-archival-data-migration/migration.bucket.name:1}}-${Environment}'
Related
I have created a cloudformation template which creates a new repo in codecommit,Also it need to pull the source.zip from S3 and copy it to the repo. but while running the template i see 400 bad request.
CF template:
AWSTemplateFormatVersion: 2010-09-09
Description: my First code commit CF template
Parameters:
DemoBucket:
Type: String
Description: Bucket in which you have code
Default: jaivijaycccf
DemoKey:
Type: String
Description: key of zipped code
Default: demo.zip
Resources:
HelloWorld:
Type: AWS::CodeCommit::Repository
Properties:
RepositoryName: HelloWorldApp
RepositoryDescription: This is a repository for my project with code from MySourceCodeBucket
Code:
BranchName: development
S3:
Bucket: !Ref DemoBucket
Key: !Ref DemoKey
ObjectVersion: 1
If the S3 bucket that is storing the source code does not use Object Versioning then providing the ObjectVersion paramater to the Cloudformation template will cause it to fail.
Removing the unnecessary parameter will fix the problem.
I'm adding this as an answer after you've confirmed that you aren't using object versioning.
I have a CFN template where in I am creating 2 s3 buckets for the image resizing using CloudFront.
the issue is that I want to use an already existing bucket from s3 for these functions.
but I get an error that s3 already exists when I provide the resource ARN and other data.
how can I resolve this?
I tried giving the details ARN name etc and tried deploying but it doesn't work
Something like this would help you:
AWSTemplateFormatVersion: '2010-09-09'
Description: 'CFN template example for referencing existing S3 bucket to lambda'
Parameters:
myS3Bucket:
Type: String
Description: Provide the S3 bucket you want to referece into your lambda.
Resources:
MyLambdaFunction:
Type: AWS::Lambda::Function
Properties:
Description: A lambda function
Handler: index.handler
Runtime: python3.7
Environment:
Variables:
S3_BUCKET: !Ref myS3Bucket
I'm trying to use Cloudformation to package and deploy a simple "hello world" serverless app that uses a single Lambda Layer. The issue I'm having is that the LayerVersion section in my CF template file doesn't seem to like the fact that I'm using a !Ref to specify the S3Bucket and S3Key values. I don't want to hard-code these; nothing I've found in the documentation suggests that what I'm trying to do won't work, but it doesn't work :(
Here's the output of the deploy command that's failing:
aws cloudformation deploy --template-file out.yml --stack-name cftest-lambda --parameter-overrides S3BucketNameParameter=cftest-0eddf3f0b289f2c2 S3LambdaLayerNameParameter=cftest-lambda-layer-1602434332.zip --capabilities CAPABILITY_NAMED_IAM
Waiting for changeset to be created..
Failed to create the changeset: Waiter ChangeSetCreateComplete failed: Waiter encountered a terminal failure state Status: FAILED. Reason: Transform AWS::Serverless-2016-10-31 failed with: Invalid Serverless Application Specification document. Number of errors found: 1. Resource with id [libs] is invalid. property Content not defined for resource of type AWS::Serverless::LayerVersion
Here is the full CF template file:
cat template.yml
AWSTemplateFormatVersion: '2010-09-09'
Transform: 'AWS::Serverless-2016-10-31'
Description: An AWS Lambda application
Parameters:
S3BucketNameParameter:
Type: String
Description: Bucket name for deployment artifacts
S3LambdaLayerNameParameter:
Type: String
Description: Object name for lambda layer deployment artifact
Resources:
helloworldfunction:
Type: AWS::Serverless::Function
Properties:
Handler: lambda_function.lambda_handler
Runtime: python3.8
CodeUri: hello-world-with-layer/.
Description: Hello world function to test cf using layers
Timeout: 10
# Function's execution role
Policies:
- AWSLambdaBasicExecutionRole
- AWSLambdaReadOnlyAccess
- AWSXrayWriteOnlyAccess
Tracing: Active
Layers:
- !Ref libs
libs:
Type: AWS::Serverless::LayerVersion
Properties:
Content:
S3Bucket: !Ref S3BucketNameParameter
S3Key: !Ref S3LambdaLayerNameParameter
CompatibleRuntimes:
- python3.8
LayerName: hello-world-lib
Description: Dependencies for the hello-world-with-layer app.
Any suggestions on how to approach this correctly?
The correct properties for LayerContent are:
Bucket: String
Key: String
Version: String
However, you are using (different names):
S3Bucket: String
S3Key: String
I'm new to SAM templates. I have the following snippet of my SAM Template where I used to pass the name of bucket name as a parameter from outside of this SAM YAML file :-
SAM Template:-
MyLambda:
Type: AWS::Serverless::Function
Properties:
CodeUri: ./functions/test/dist/
Handler: index.lambdaHandler
Runtime: nodejs12.x
Events:
S3PutObjectEvent:
Type: S3
Properties:
Bucket: !Ref S3BucketName
Events: s3:ObjectCreated:*
Parameter.YAML:-
DeploymentEnvironment:
default:
S3BucketName: my-awesome-s3-bucket
Now, I do not create any S3 Bucket using SAM Template or Infrastructure as a code (IaC). Bucket creation is done by Lambda code itself hence there is no S3 Object Type declaration in my SAM Template.
When I execute this command, sam validate to validate the SAM Template then I get this error:-
/template.yaml' was invalid SAM Template.
Error: [InvalidResourceException('MyLambda', 'Event with id [S3PutObjectEvent] is invalid. S3 events must reference an S3 bucket in the same template.')] ('MyLambda', 'Event with id [S3PutObjectEvent] is invalid. S3 events must reference an S3 bucket in the same template.')
I really need your help in achieving this as I tried hard in getting it solved. I read various forums, not sure if we can pass the bucket name from outside of the SAM template or not.
Is there any way workaround? This is really critical issue for me. Appreciate your help on this. thanks
Bucket creation is done by Lambda code itself
I'd recommend against this pattern, as your Lambda even source won't get created if the Bucket doesn't already exist.
Try creating the bucket in your SAM template, and pass the bucket name to your function as an environment variable.
Optionally you can set different environment names on your bucket name (addressing comment) using Parameters.
Parameters:
Env:
Type: String
AllowedValues:
- dev
- qa
- prod
Default: dev
Resources:
MyBucket:
Type: AWS::S3::Bucket
Properties:
BucketName: !Sub 'My-unique-bucket-name-${Env}'
MyLambda:
Type: AWS::Serverless::Function
Properties:
CodeUri: ./functions/test/dist/
Handler: index.lambdaHandler
Runtime: nodejs12.x
Environment:
Variables:
BUCKET_NAME: !Ref MyBucket # passed to Lambda as environment variable
Events:
S3PutObjectEvent:
Type: S3
Properties:
Bucket: !Ref MyBucket
Events: s3:ObjectCreated:*
And get the bucket name in your function
const bucket = process.env.BUCKET_NAME
I'm trying to create multiple S3 bucktes with same propeties.But I'm not able to create multiple s3 buckets.
I found in http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/resources-section-structure.html
if you have multiple resources of the same type, you can declare them together by separating them with commas
But I didn't find any example and I'm not sure how to do it.I tried debugging but not getting the result.
Please suggest.
Below is my yaml file:
AWSTemplateFormatVersion: '2010-09-09'
Resources:
myS3Bucketlo:
Type: AWS::S3::Bucket
Properties:
AccessControl: AuthenticatedRead
Outputs:
WebsiteURL:
Value: !GetAtt myS3Bucketlo.WebsiteURL
Description: URL for the website hosted on S3
In a CloudFormation template, each resource must be declared separately. So, even if your buckets have identical properties, they still must be individually declared:
AWSTemplateFormatVersion: '2010-09-09'
Resources:
bucket1:
Type: AWS::S3::Bucket
Properties:
AccessControl: AuthenticatedRead
bucket2:
Type: AWS::S3::Bucket
Properties:
AccessControl: AuthenticatedRead
bucket3:
Type: AWS::S3::Bucket
Properties:
AccessControl: AuthenticatedRead
Outputs:
WebsiteURL1:
Value: !GetAtt bucket1.WebsiteURL
Description: URL for the website 1 hosted on S3
WebsiteURL2:
Value: !GetAtt bucket2.WebsiteURL
Description: URL for the website 2 hosted on S3
WebsiteURL3:
Value: !GetAtt bucket3.WebsiteURL
Description: URL for the website 3 hosted on S3
However,
You must declare each resource separately; however, if you have multiple resources of the same type, you can declare them together by separating them with commas.
The wording of this text does imply there is a shortcut to avoid duplication, but I have never seen such a working example.