aws-cdk-lib
Version:
Version 2 of the AWS Cloud Development Kit library
2 lines (1 loc) • 8.03 kB
JavaScript
;Object.defineProperty(exports,"__esModule",{value:!0}),exports.LoggingDestination=exports.LogType=void 0,exports.configureTracingDelivery=configureTracingDelivery,exports.configureLoggingDelivery=configureLoggingDelivery;var jsiiDeprecationWarnings=()=>{var tmp=require("../../../.warnings.jsii.js");return jsiiDeprecationWarnings=()=>tmp,tmp};const JSII_RTTI_SYMBOL_1=Symbol.for("jsii.rtti");var constructs_1=()=>{var tmp=require("constructs");return constructs_1=()=>tmp,tmp},aws_iam_1=()=>{var tmp=require("../../../aws-iam");return aws_iam_1=()=>tmp,tmp},logs=()=>{var tmp=require("../../../aws-logs");return logs=()=>tmp,tmp},xray=()=>{var tmp=require("../../../aws-xray");return xray=()=>tmp,tmp},core_1=()=>{var tmp=require("../../../core");return core_1=()=>tmp,tmp};const MAX_DELIVERY_NAME_LENGTH=60;class LogType{value;static[JSII_RTTI_SYMBOL_1]={fqn:"aws-cdk-lib.aws_bedrockagentcore.LogType",version:"2.257.0"};static APPLICATION_LOGS=new LogType("APPLICATION_LOGS");static USAGE_LOGS=new LogType("USAGE_LOGS");static of(value){return new LogType(value)}constructor(value){this.value=value}}exports.LogType=LogType;class LoggingDestination{static[JSII_RTTI_SYMBOL_1]={fqn:"aws-cdk-lib.aws_bedrockagentcore.LoggingDestination",version:"2.257.0"};static cloudWatchLogs(logGroup){try{jsiiDeprecationWarnings().aws_cdk_lib_aws_logs_ILogGroup(logGroup)}catch(error){throw process.env.JSII_DEBUG!=="1"&&error.name==="DeprecationError"&&Error.captureStackTrace(error,this.cloudWatchLogs),error}return new CloudWatchLogsDestination(logGroup)}static s3(bucket){try{jsiiDeprecationWarnings().aws_cdk_lib_aws_s3_IBucket(bucket)}catch(error){throw process.env.JSII_DEBUG!=="1"&&error.name==="DeprecationError"&&Error.captureStackTrace(error,this.s3),error}return new S3Destination(bucket)}static firehose(stream){try{jsiiDeprecationWarnings().aws_cdk_lib_aws_kinesisfirehose_IDeliveryStream(stream)}catch(error){throw process.env.JSII_DEBUG!=="1"&&error.name==="DeprecationError"&&Error.captureStackTrace(error,this.firehose),error}return new FirehoseDestination(stream)}}exports.LoggingDestination=LoggingDestination;class CloudWatchLogsDestination extends LoggingDestination{logGroup;constructor(logGroup){super(),this.logGroup=logGroup}_bind(scope,id){const stack=core_1().Stack.of(scope),policyId="CdkLogGroupLogsDeliveryPolicy";let resourcePolicy=stack.node.tryFindChild(policyId);resourcePolicy||(resourcePolicy=new(logs()).ResourcePolicy(stack,policyId)),resourcePolicy.document.addStatements(new(aws_iam_1()).PolicyStatement({effect:aws_iam_1().Effect.ALLOW,principals:[new(aws_iam_1()).ServicePrincipal("delivery.logs.amazonaws.com")],actions:["logs:CreateLogStream","logs:PutLogEvents"],resources:[`${this.logGroup.logGroupArn}:log-stream:*`],conditions:{StringEquals:{"aws:SourceAccount":stack.account},ArnLike:{"aws:SourceArn":stack.formatArn({service:"logs",resource:"*"})}}}));const deliveryDestination=new(logs()).CfnDeliveryDestination(scope,`${id}Dest`,{name:core_1().Lazy.string({produce:()=>core_1().Names.uniqueResourceName(deliveryDestination,{maxLength:MAX_DELIVERY_NAME_LENGTH})}),deliveryDestinationType:"CWL",destinationResourceArn:this.logGroup.logGroupArn});return deliveryDestination.node.addDependency(resourcePolicy),{deliveryDestination}}}class S3Destination extends LoggingDestination{bucket;constructor(bucket){super(),this.bucket=bucket}_bind(scope,id){const stack=core_1().Stack.of(scope);this.bucket.addToResourcePolicy(new(aws_iam_1()).PolicyStatement({effect:aws_iam_1().Effect.ALLOW,principals:[new(aws_iam_1()).ServicePrincipal("delivery.logs.amazonaws.com")],actions:["s3:PutObject"],resources:[`${this.bucket.bucketArn}/AWSLogs/${stack.account}/*`],conditions:{StringEquals:{"s3:x-amz-acl":"bucket-owner-full-control","aws:SourceAccount":stack.account},ArnLike:{"aws:SourceArn":stack.formatArn({service:"logs",resource:"delivery-source",resourceName:"*",arnFormat:core_1().ArnFormat.COLON_RESOURCE_NAME})}}}));const deliveryDestination=new(logs()).CfnDeliveryDestination(scope,`${id}Dest`,{name:core_1().Lazy.string({produce:()=>core_1().Names.uniqueResourceName(deliveryDestination,{maxLength:MAX_DELIVERY_NAME_LENGTH})}),deliveryDestinationType:"S3",destinationResourceArn:this.bucket.bucketArn});return{deliveryDestination}}}class FirehoseDestination extends LoggingDestination{stream;constructor(stream){super(),this.stream=stream}_bind(scope,id){core_1().Tags.of(this.stream).add("LogDeliveryEnabled","true");const deliveryDestination=new(logs()).CfnDeliveryDestination(scope,`${id}Dest`,{name:core_1().Lazy.string({produce:()=>core_1().Names.uniqueResourceName(deliveryDestination,{maxLength:MAX_DELIVERY_NAME_LENGTH})}),deliveryDestinationType:"FH",destinationResourceArn:this.stream.deliveryStreamArn});return{deliveryDestination}}}class XRayResourcePolicy extends constructs_1().Construct{document;constructor(scope,id){super(scope,id),this.document=new(aws_iam_1()).PolicyDocument,new(xray()).CfnResourcePolicy(this,"ResourcePolicy",{policyName:core_1().Lazy.string({produce:()=>core_1().Names.uniqueResourceName(this,{maxLength:128})}),policyDocument:core_1().Lazy.string({produce:()=>JSON.stringify(this.document.toJSON())})})}}function configureTracingDelivery(scope,sourceArn){const stack=core_1().Stack.of(scope),deliverySource=new(logs()).CfnDeliverySource(scope,"TracesDeliverySource",{name:core_1().Lazy.string({produce:()=>core_1().Names.uniqueResourceName(deliverySource,{maxLength:MAX_DELIVERY_NAME_LENGTH})}),logType:"TRACES",resourceArn:sourceArn}),policyId="CdkXRayLogsDeliveryPolicy";let xrayPolicy=stack.node.tryFindChild(policyId);xrayPolicy||(xrayPolicy=new XRayResourcePolicy(stack,policyId)),xrayPolicy.document.addStatements(new(aws_iam_1()).PolicyStatement({effect:aws_iam_1().Effect.ALLOW,principals:[new(aws_iam_1()).ServicePrincipal("delivery.logs.amazonaws.com")],actions:["xray:PutTraceSegments"],resources:["*"],conditions:{"ForAllValues:ArnLike":{"logs:LogGeneratingResourceArns":[sourceArn]},StringEquals:{"aws:SourceAccount":stack.account},ArnLike:{"aws:SourceArn":stack.formatArn({service:"logs",resource:"delivery-source",resourceName:"*",arnFormat:core_1().ArnFormat.COLON_RESOURCE_NAME})}}}));const deliveryDestination=new(logs()).CfnDeliveryDestination(scope,"TracesDeliveryDest",{name:core_1().Lazy.string({produce:()=>core_1().Names.uniqueResourceName(deliveryDestination,{maxLength:MAX_DELIVERY_NAME_LENGTH})}),deliveryDestinationType:"XRAY"});deliveryDestination.node.addDependency(xrayPolicy);const delivery=new(logs()).CfnDelivery(scope,"TracesDelivery",{deliverySourceName:deliverySource.deliverySourceRef.deliverySourceName,deliveryDestinationArn:deliveryDestination.attrArn});return delivery.node.addDependency(deliverySource),delivery.node.addDependency(deliveryDestination),delivery}function configureLoggingDelivery(scope,sourceArn,loggingConfigs){const deliveries=[],configsByLogType=new Map;for(const config of loggingConfigs){const existing=configsByLogType.get(config.logType.value)??[];existing.push(config),configsByLogType.set(config.logType.value,existing)}for(const[logType,configs]of configsByLogType){const logTypeId=logType.split("_").map(word=>word.charAt(0)+word.slice(1).toLowerCase()).join(""),deliverySource=new(logs()).CfnDeliverySource(scope,`${logTypeId}DeliverySource`,{name:core_1().Lazy.string({produce:()=>core_1().Names.uniqueResourceName(deliverySource,{maxLength:MAX_DELIVERY_NAME_LENGTH})}),logType,resourceArn:sourceArn});configs.forEach((config,index)=>{const id=configs.length===1?logTypeId:`${logTypeId}${index}`,bindConfig=config.destination._bind(scope,id),delivery=new(logs()).CfnDelivery(scope,`${id}Delivery`,{deliverySourceName:deliverySource.deliverySourceRef.deliverySourceName,deliveryDestinationArn:bindConfig.deliveryDestination.attrArn});delivery.node.addDependency(deliverySource),delivery.node.addDependency(bindConfig.deliveryDestination),deliveries.length>0&&delivery.node.addDependency(deliveries[deliveries.length-1]),deliveries.push(delivery)})}return deliveries}