HOw to invalidate CloudFront distribution when deploying using AWS CDK #20484
-
I am deploying my S3 based site using AWS CDK (.NET). new BucketDeployment(this,
$"{props.BucketPrefix}BucketDeployment",
new BucketDeploymentProps
{
Sources = new ISource[]
{
Source.Bucket(
props.SourceBucket,
$"{props.BuildVersion}/{props.SiteArtifactsZipFileName}")
},
DestinationBucket = Bucket,
RetainOnDelete = false
}); next I am creating a Distribution and adding Behaviors: // create a tenant specific distribution
var distribution = new Distribution(this,
$"{props.TenantName}{props.TargetBucket}Distribution",
new DistributionProps
{
Certificate = Certificate.FromCertificateArn(this,
$"{props.TenantName}{props.TargetBucket}CertificateLookup",
certificateArn),
DomainNames = new string[] { siteDomain },
MinimumProtocolVersion = SecurityPolicyProtocol.TLS_V1_2_2021,
HttpVersion = HttpVersion.HTTP2,
ErrorResponses = new ErrorResponse[]
{
new ErrorResponse
{
HttpStatus = 403,
Ttl = Duration.Seconds(300),
ResponsePagePath = "/index.html",
ResponseHttpStatus = 200
},
new ErrorResponse
{
HttpStatus = 404,
Ttl = Duration.Seconds(300),
ResponsePagePath = "/index.html",
ResponseHttpStatus = 200
},
},
DefaultBehavior = new BehaviorOptions
{
Origin = new S3Origin(props.TargetBucket),
ViewerProtocolPolicy = ViewerProtocolPolicy.REDIRECT_TO_HTTPS
},
GeoRestriction = GeoRestriction.Allowlist(new string[]
{
"CA",
"US"
}),
Comment = $"{props.PortalName} {props.TenantName} {props.EnvironmentName} Distribution"
});
Tags
.Of(distribution)
.Add("Accounting", "Teller Online");
new ARecord(this,
$"{props.TenantName}{props.TargetBucket}ARecord",
new ARecordProps
{
RecordName = siteDomain,
Target = RecordTarget.FromAlias(new CloudFrontTarget(distribution)),
Zone = zone
});
// add custom API origin and behaviour
distribution.AddBehavior(
"/api/*",
new HttpOrigin(
Fn.Select(2, Fn.Split("/", props.CustomOriginLambdaBaseUrl)),
new HttpOriginProps
{
OriginPath = "/prod",
OriginSslProtocols = new OriginSslPolicy[] { OriginSslPolicy.TLS_V1_2 },
ProtocolPolicy = OriginProtocolPolicy.HTTPS_ONLY,
CustomHeaders = new Dictionary<string, string>
{
{ "TenantName", props.TenantName }
}
}),
new BehaviorOptions
{
ViewerProtocolPolicy = ViewerProtocolPolicy.REDIRECT_TO_HTTPS,
AllowedMethods = AllowedMethods.ALLOW_ALL
}
); How can I invalidate this Distribution after adding/modifying it (or rather its source)? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
You can use the distributionPaths prop to invalidate files with the BucketDeployment construct, see more here |
Beta Was this translation helpful? Give feedback.
-
Hello! Reopening this discussion to make it searchable. |
Beta Was this translation helpful? Give feedback.
You can use the distributionPaths prop to invalidate files with the BucketDeployment construct, see more here