Skip to content

Commit

Permalink
feat(apprunner): add ObservabilityConfiguration for AppRunner Service (
Browse files Browse the repository at this point in the history
…#30359)

### Issue # (if applicable)

Closes #22985 .

### Reason for this change
At the moment, L2 Construct does not support a tracing setting for the AppRunner Service.


### Description of changes
* Add `ObservabilityConfiguration` Class
* Add `observabilityConfiguration` property to the `Service` Class



### Description of how you validated changes
Add unit tests and integ tests.


### Checklist
- [x] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md)
----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
mazyu36 authored Jun 24, 2024
1 parent 6ef7c67 commit 9e9cc27
Show file tree
Hide file tree
Showing 15 changed files with 914 additions and 0 deletions.
19 changes: 19 additions & 0 deletions packages/@aws-cdk/aws-apprunner-alpha/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -296,3 +296,22 @@ new apprunner.Service(this, 'Service', {
}),
});
```

## Observability Configuration

To associate an App Runner service with a custom observability configuration, use the `observabilityConfiguration` property.

```ts
const observabilityConfiguration = new apprunner.ObservabilityConfiguration(this, 'ObservabilityConfiguration', {
observabilityConfigurationName: 'MyObservabilityConfiguration',
traceConfigurationVendor: apprunner.TraceConfigurationVendor.AWSXRAY,
});

new apprunner.Service(this, 'DemoService', {
source: apprunner.Source.fromEcrPublic({
imageConfiguration: { port: 8000 },
imageIdentifier: 'public.ecr.aws/aws-containers/hello-app-runner:latest',
}),
observabilityConfiguration,
});
```
1 change: 1 addition & 0 deletions packages/@aws-cdk/aws-apprunner-alpha/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// AWS::AppRunner CloudFormation Resources:
export * from './auto-scaling-configuration';
export * from './observability-configuration';
export * from './service';
export * from './vpc-connector';
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
import * as cdk from 'aws-cdk-lib/core';
import { Construct } from 'constructs';
import { CfnObservabilityConfiguration } from 'aws-cdk-lib/aws-apprunner';

/**
* The implementation provider chosen for tracing App Runner services
*
* @see https://docs.aws.amazon.com/apprunner/latest/dg/monitor.html
*/
export enum TraceConfigurationVendor {
/**
* Tracing (X-Ray)
*/
AWSXRAY = 'AWSXRAY',
}

/**
* Properties of the AppRunner Observability configuration
*/
export interface ObservabilityConfigurationProps {
/**
* The name for the ObservabilityConfiguration.
*
* @default - a name generated by CloudFormation
*/
readonly observabilityConfigurationName?: string;

/**
* The implementation provider chosen for tracing App Runner services.
*/
readonly traceConfigurationVendor: TraceConfigurationVendor;
}

/**
* Attributes for the App Runner Observability configuration
*/
export interface ObservabilityConfigurationAttributes {
/**
* The name of the Observability configuration.
*/
readonly observabilityConfigurationName: string;

/**
* The revision of the Observability configuration.
*/
readonly observabilityConfigurationRevision: number;
}

/**
* Represents the App Runner Observability configuration.
*/
export interface IObservabilityConfiguration extends cdk.IResource {
/**
* The Name of the Observability configuration.
* @attribute
*/
readonly observabilityConfigurationName: string;

/**
* The ARN of the Observability configuration.
* @attribute
*/
readonly observabilityConfigurationArn: string;

/**
* The revision of the Observability configuration.
* @attribute
*/
readonly observabilityConfigurationRevision: number;
}

/**
* The App Runner Observability configuration
*
* @resource AWS::AppRunner::ObservabilityConfiguration
*/
export class ObservabilityConfiguration extends cdk.Resource implements IObservabilityConfiguration {
/**
* Imports an App Runner Observability Configuration from attributes.
*/
public static fromObservabilityConfigurationAttributes(scope: Construct, id: string,
attrs: ObservabilityConfigurationAttributes): IObservabilityConfiguration {
const observabilityConfigurationName = attrs.observabilityConfigurationName;
const observabilityConfigurationRevision = attrs.observabilityConfigurationRevision;

class Import extends cdk.Resource implements IObservabilityConfiguration {
public readonly observabilityConfigurationName = observabilityConfigurationName;
public readonly observabilityConfigurationRevision = observabilityConfigurationRevision;
public readonly observabilityConfigurationArn = cdk.Stack.of(this).formatArn({
resource: 'observabilityconfiguration',
service: 'apprunner',
resourceName: `${attrs.observabilityConfigurationName}/${attrs.observabilityConfigurationRevision}`,
});
}

return new Import(scope, id);
}

/**
* Imports an App Runner Observability Configuration from its ARN
*/
public static fromArn(scope: Construct, id: string, observabilityConfigurationArn: string): IObservabilityConfiguration {
const resourceParts = cdk.Fn.split('/', observabilityConfigurationArn);

if (!resourceParts || resourceParts.length < 3) {
throw new Error(`Unexpected ARN format: ${observabilityConfigurationArn}`);
}

const observabilityConfigurationName = cdk.Fn.select(0, resourceParts);
const observabilityConfigurationRevision = Number(cdk.Fn.select(1, resourceParts));

class Import extends cdk.Resource implements IObservabilityConfiguration {
public readonly observabilityConfigurationName = observabilityConfigurationName;
public readonly observabilityConfigurationRevision = observabilityConfigurationRevision;
public readonly observabilityConfigurationArn = observabilityConfigurationArn;
}

return new Import(scope, id);
}

/**
* The ARN of the Observability configuration.
* @attribute
*/
readonly observabilityConfigurationArn: string;

/**
* The revision of the Observability configuration.
* @attribute
*/
readonly observabilityConfigurationRevision: number;

/**
* The name of the Observability configuration.
* @attribute
*/
readonly observabilityConfigurationName: string;

public constructor(scope: Construct, id: string, props: ObservabilityConfigurationProps) {
super(scope, id, {
physicalName: props.observabilityConfigurationName,
});

if (
props.observabilityConfigurationName !== undefined &&
!cdk.Token.isUnresolved(props.observabilityConfigurationName) &&
!/^[A-Za-z0-9][A-Za-z0-9\-_]{3,31}$/.test(props.observabilityConfigurationName)
) {
throw new Error(`observabilityConfigurationName must match the \`^[A-Za-z0-9][A-Za-z0-9\-_]{3,31}$\` pattern, got ${props.observabilityConfigurationName}`);
}

const resource = new CfnObservabilityConfiguration(this, 'Resource', {
observabilityConfigurationName: props.observabilityConfigurationName,
traceConfiguration: {
vendor: props.traceConfigurationVendor,
},
});

this.observabilityConfigurationArn = resource.attrObservabilityConfigurationArn;
this.observabilityConfigurationRevision = resource.attrObservabilityConfigurationRevision;
this.observabilityConfigurationName = resource.ref;
}
}
13 changes: 13 additions & 0 deletions packages/@aws-cdk/aws-apprunner-alpha/lib/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { Construct } from 'constructs';
import { CfnService } from 'aws-cdk-lib/aws-apprunner';
import { IVpcConnector } from './vpc-connector';
import { IAutoScalingConfiguration } from './auto-scaling-configuration';
import { IObservabilityConfiguration } from './observability-configuration';

/**
* The image repository types
Expand Down Expand Up @@ -743,6 +744,14 @@ export interface ServiceProps {
* @default - IpAddressType.IPV4
*/
readonly ipAddressType?: IpAddressType;

/**
* Settings for an App Runner observability configuration.
*
* @default - no observability configuration resource is associated with the service.
*/
readonly observabilityConfiguration?: IObservabilityConfiguration;

}

/**
Expand Down Expand Up @@ -1296,6 +1305,10 @@ export class Service extends cdk.Resource implements iam.IGrantable {
healthCheckConfiguration: this.props.healthCheck ?
this.props.healthCheck.bind() :
undefined,
observabilityConfiguration: props.observabilityConfiguration ? {
observabilityEnabled: true,
observabilityConfigurationArn: props.observabilityConfiguration.observabilityConfigurationArn,
} : undefined,
});

// grant required privileges for the role
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 9e9cc27

Please sign in to comment.