Skip to content
v2.274.0

InstanceProfile L2 construct ​

IAM Instance Profile.

Example ​

ts
declare const vpc: ec2.Vpc;

const role = new iam.Role(this, 'Role', {
  assumedBy: new iam.ServicePrincipal('ec2.amazonaws.com'),
});
const instanceProfile = new iam.InstanceProfile(this, 'InstanceProfile', {
  role,
});

const template = new ec2.LaunchTemplate(this, 'LaunchTemplate', {
  launchTemplateName: 'MyTemplateV1',
  versionDescription: 'This is my v1 template',
  machineImage: ec2.MachineImage.latestAmazonLinux2023(),
  securityGroup: new ec2.SecurityGroup(this, 'LaunchTemplateSG', {
    vpc: vpc,
  }),
  instanceProfile,
});
python
# vpc: ec2.Vpc


role = iam.Role(self, "Role",
    assumed_by=iam.ServicePrincipal("ec2.amazonaws.com")
)
instance_profile = iam.InstanceProfile(self, "InstanceProfile",
    role=role
)

template = ec2.LaunchTemplate(self, "LaunchTemplate",
    launch_template_name="MyTemplateV1",
    version_description="This is my v1 template",
    machine_image=ec2.MachineImage.latest_amazon_linux2023(),
    security_group=ec2.SecurityGroup(self, "LaunchTemplateSG",
        vpc=vpc
    ),
    instance_profile=instance_profile
)
java
Vpc vpc;


Role role = Role.Builder.create(this, "Role")
        .assumedBy(new ServicePrincipal("ec2.amazonaws.com"))
        .build();
InstanceProfile instanceProfile = InstanceProfile.Builder.create(this, "InstanceProfile")
        .role(role)
        .build();

LaunchTemplate template = LaunchTemplate.Builder.create(this, "LaunchTemplate")
        .launchTemplateName("MyTemplateV1")
        .versionDescription("This is my v1 template")
        .machineImage(MachineImage.latestAmazonLinux2023())
        .securityGroup(SecurityGroup.Builder.create(this, "LaunchTemplateSG")
                .vpc(vpc)
                .build())
        .instanceProfile(instanceProfile)
        .build();
csharp
// Install: dotnet add package Amazon.CDK.Lib
Vpc vpc;


var role = new Role(this, "Role", new RoleProps {
    AssumedBy = new ServicePrincipal("ec2.amazonaws.com")
});
var instanceProfile = new InstanceProfile(this, "InstanceProfile", new InstanceProfileProps {
    Role = role
});

var template = new LaunchTemplate(this, "LaunchTemplate", new LaunchTemplateProps {
    LaunchTemplateName = "MyTemplateV1",
    VersionDescription = "This is my v1 template",
    MachineImage = MachineImage.LatestAmazonLinux2023(),
    SecurityGroup = new SecurityGroup(this, "LaunchTemplateSG", new SecurityGroupProps {
        Vpc = vpc
    }),
    InstanceProfile = instanceProfile
});
go
// Install: go get github.com/aws/aws-cdk-go/awscdk/v2
var vpc Vpc


role := iam.NewRole(this, jsii.String("Role"), &RoleProps{
	AssumedBy: iam.NewServicePrincipal(jsii.String("ec2.amazonaws.com")),
})
instanceProfile := iam.NewInstanceProfile(this, jsii.String("InstanceProfile"), &InstanceProfileProps{
	Role: Role,
})

template := ec2.NewLaunchTemplate(this, jsii.String("LaunchTemplate"), &LaunchTemplateProps{
	LaunchTemplateName: jsii.String("MyTemplateV1"),
	VersionDescription: jsii.String("This is my v1 template"),
	MachineImage: ec2.MachineImage_LatestAmazonLinux2023(),
	SecurityGroup: ec2.NewSecurityGroup(this, jsii.String("LaunchTemplateSG"), &SecurityGroupProps{
		Vpc: vpc,
	}),
	InstanceProfile: InstanceProfile,
})

Initializer ​

ts
new InstanceProfile(scope: Construct, id: string, props?: InstanceProfileProps)
python
InstanceProfile(scope: Construct, id: str, props: typing.Optional[InstanceProfileProps] = None)
java
new InstanceProfile(Construct scope, String id, InstanceProfileProps props)
csharp
new InstanceProfile(Construct scope, string id, InstanceProfileProps? props = null)
go
NewInstanceProfile(Scope Construct, Id *string, Props InstanceProfileProps)

Construct Props ​

instanceProfileNameoptionalstringinstance_profile_nameoptionalstrinstanceProfileNameoptionalStringInstanceProfileNameoptionalstringInstanceProfileNameoptional*string#
The name of the InstanceProfile to create.

Default: generated by CloudFormation

pathoptionalstringpathoptionalstrpathoptionalStringPathoptionalstringPathoptional*string= /#
The path to the InstanceProfile.
roleoptionalIRoleroleoptionalIRoleroleoptionalIRoleRoleoptionalIRoleRoleoptionalIRole#
An IAM role to associate with the instance profile that is used by EC2 instances.

Default: a role will be automatically created, it can be accessed via the role property

The role must be assumable by the service principal ec2.amazonaws.com:

example
ts
const role = new iam.Role(this, 'MyRole', {
  assumedBy: new iam.ServicePrincipal('ec2.amazonaws.com')
});

Properties ​

The environment this resource belongs to.
notes

For resources that are created and managed in a Stack (those created by creating new class instances like new Role(), new Bucket(), etc.), this is always the same as the environment of the stack they belong to.

For referenced resources (those obtained from referencing methods like Role.fromRoleArn(), Bucket.fromBucketName(), etc.), they might be different than the stack they were imported into.

instanceProfileArnstringinstance_profile_arnstrinstanceProfileArnStringInstanceProfileArnstringInstanceProfileArn*stringrequired#
Returns the ARN of this InstanceProfile.
instanceProfileNamestringinstance_profile_namestrinstanceProfileNameStringInstanceProfileNamestringInstanceProfileName*stringrequired#
Returns the name of this InstanceProfile.
instanceProfileRefInstanceProfileReferenceinstance_profile_refInstanceProfileReferenceinstanceProfileRefInstanceProfileReferenceInstanceProfileRefInstanceProfileReferenceInstanceProfileRefInstanceProfileReferencerequired#
A reference to a InstanceProfile resource.
nodeNodenodeNodenodeNodeNodeNodeNodeNoderequired#
The tree node.
PROPERTY_INJECTION_IDstringproperty_injection_idstrPROPERTY_INJECTION_IDStringPROPERTY_INJECTION_IDstringPROPERTY_INJECTION_ID*stringrequiredstatic#
Uniquely identifies this class.
rolemay be undefinedIRolerolemay be undefinedIRolerolemay be undefinedIRoleRolemay be undefinedIRoleRolemay be undefinedIRole#
Returns the role associated with this InstanceProfile.
stackStackstackStackstackStackStackStackStackStackrequired#
The stack in which this resource is defined.

Methods ​

applyCrossStackReferenceStrength(strength: ReferenceStrength): voiddef apply_cross_stack_reference_strength(self, strength: ReferenceStrength) -> Nonepublic void applyCrossStackReferenceStrength(ReferenceStrength strength)public void ApplyCrossStackReferenceStrength(ReferenceStrength strength)func ApplyCrossStackReferenceStrength(Strength ReferenceStrength)#
Override the cross-stack reference strength for this resource.
notes · 1 parameter

When set, any cross-stack reference to this resource will use the specified mechanism instead of the global default determined by the @aws-cdk/core:defaultCrossStackReferences context key. This is useful for selectively weakening specific references to avoid the "deadly embrace" problem without changing the app-wide default.

Parameters

  • strengthstrengthstrengthstrengthStrength — - The reference strength to use for this resource.
applyRemovalPolicy(policy: RemovalPolicy): voiddef apply_removal_policy(self, policy: RemovalPolicy) -> Nonepublic void applyRemovalPolicy(RemovalPolicy policy)public void ApplyRemovalPolicy(RemovalPolicy policy)func ApplyRemovalPolicy(Policy RemovalPolicy)#
Apply the given removal policy to this resource.
notes

The Removal Policy controls what happens to this resource when it stops being managed by CloudFormation, either because you've removed it from the CDK application or because you've made a change that requires the resource to be replaced.

The resource can be deleted (RemovalPolicy.DESTROY), or left in your AWS account for data recovery and cleanup later (RemovalPolicy.RETAIN).

fromInstanceProfileArn(scope: Construct, id: string, instanceProfileArn: string): IInstanceProfile@staticmethod def from_instance_profile_arn(scope: Construct, id: str, instance_profile_arn: str) -> IInstanceProfilepublic static IInstanceProfile fromInstanceProfileArn(Construct scope, String id, String instanceProfileArn)public static IInstanceProfile FromInstanceProfileArn(Construct scope, string id, string instanceProfileArn)func FromInstanceProfileArn(Scope Construct, Id *string, InstanceProfileArn *string) IInstanceProfilestatic#
Import an existing InstanceProfile from an InstanceProfile ARN.

If the ARN comes from a Token, the InstanceProfile cannot have a path; if so, any attempt to reference its instanceProfileName will fail.

3 parameters

Parameters

  • scopescopescopescopeScope — construct scope.
  • ididididId — construct id.
  • instanceProfileArninstance_profile_arninstanceProfileArninstanceProfileArnInstanceProfileArn — the ARN of the exiting InstanceProfile to import.
fromInstanceProfileAttributes(scope: Construct, id: string, attrs: InstanceProfileAttributes): IInstanceProfile@staticmethod def from_instance_profile_attributes(scope: Construct, id: str, attrs: InstanceProfileAttributes) -> IInstanceProfilepublic static IInstanceProfile fromInstanceProfileAttributes(Construct scope, String id, InstanceProfileAttributes attrs)public static IInstanceProfile FromInstanceProfileAttributes(Construct scope, string id, InstanceProfileAttributes attrs)func FromInstanceProfileAttributes(Scope Construct, Id *string, Attrs InstanceProfileAttributes) IInstanceProfilestatic#
Import an existing InstanceProfile from given InstanceProfile attributes.

If the ARN comes from a Token, the InstanceProfile cannot have a path; if so, any attempt to reference its instanceProfileName will fail.

3 parameters

Parameters

  • scopescopescopescopeScope — construct scope.
  • ididididId — construct id.
  • attrsattrsattrsattrsAttrs — the attributes of the InstanceProfile to import.
fromInstanceProfileName(scope: Construct, id: string, instanceProfileName: string): IInstanceProfile@staticmethod def from_instance_profile_name(scope: Construct, id: str, instance_profile_name: str) -> IInstanceProfilepublic static IInstanceProfile fromInstanceProfileName(Construct scope, String id, String instanceProfileName)public static IInstanceProfile FromInstanceProfileName(Construct scope, string id, string instanceProfileName)func FromInstanceProfileName(Scope Construct, Id *string, InstanceProfileName *string) IInstanceProfilestatic#
Import an existing InstanceProfile from an InstanceProfile name.
3 parameters

Parameters

  • scopescopescopescopeScope — construct scope.
  • ididididId — construct id.
  • instanceProfileNameinstance_profile_nameinstanceProfileNameinstanceProfileNameInstanceProfileName — the name of the existing InstanceProfile to import.
isConstruct(x: any): boolean@staticmethod def is_construct(x: Any) -> boolpublic static Boolean isConstruct(Object x)public static bool IsConstruct(object x)func IsConstruct(X interface{}) *boolstatic#
Checks if `x` is a construct.
notes · 1 parameter · returns

Use this method instead of instanceof to properly detect Construct instances, even when the construct library is symlinked.

Explanation: in JavaScript, multiple copies of the constructs library on disk are seen as independent, completely different libraries. As a consequence, the class Construct in each copy of the constructs library is seen as a different class, and an instance of one class will not test as instanceof the other class. npm install will not create installations like this, but users may manually symlink construct libraries together or use a monorepo tool: in those cases, multiple copies of the constructs library can be accidentally installed, and instanceof will behave unpredictably. It is safest to avoid using instanceof, and using this type-testing method instead.

Parameters

  • xxxxX — Any object.

Returns: true if x is an object created from a class which extends Construct.

isOwnedResource(construct: IConstruct): boolean@staticmethod def is_owned_resource(construct: IConstruct) -> boolpublic static Boolean isOwnedResource(IConstruct construct)public static bool IsOwnedResource(IConstruct construct)func IsOwnedResource(Construct IConstruct) *boolstatic#
Returns true if the construct was created by CDK, and false otherwise.
isResource(construct: IConstruct): boolean@staticmethod def is_resource(construct: IConstruct) -> boolpublic static Boolean isResource(IConstruct construct)public static bool IsResource(IConstruct construct)func IsResource(Construct IConstruct) *boolstatic#
Check whether the given construct is a Resource.
toString(): stringdef to_string(self, ) -> strpublic String toString()public string ToString()func ToString() *string#
Returns a string representation of this construct.
with(...mixins: IMixin[]): IConstructdef with(self, mixins: IMixin) -> IConstructpublic IConstruct with(IMixin mixins)public IConstruct With(IMixin mixins)func With(Mixins IMixin) IConstruct#
Applies one or more mixins to this construct.

Mixins are applied in order. The list of constructs is captured at the start of the call, so constructs added by a mixin will not be visited. Use multiple with() calls if subsequent mixins should apply to added constructs.