Le traduzioni sono generate tramite traduzione automatica. In caso di conflitto tra il contenuto di una traduzione e la versione originale in Inglese, quest'ultima prevarrà.
AWS Identity and Access Management frammenti di modello
Questa sezione contiene frammenti di AWS Identity and Access Management modello.
Argomenti
Importante
Durante la creazione o l'aggiornamento di uno stack con un modello contenente risorse IAM, devi prendere atto dell'utilizzo delle funzionalità IAM. Per ulteriori informazioni, consulta Accettazione delle risorse IAM nei modelli CloudFormation.
Dichiarazione di una risorsa utente IAM
Questo frammento di codice mostra come dichiarare una risorsa AWS::IAM::User per creare un utente IAM. L'utente viene dichiarato con il percorso ("/"
) e un profilo di login che utilizza la password (myP@ssW0rd
).
Il documento di policy giveaccesstoqueueonly
consente all'utente di eseguire tutte le operazioni di Amazon SQS nella risorsa della coda Amazon SQS myqueue
e rifiuta l'accesso a tutte le altre risorse della coda Amazon SQS. La funzione Fn::GetAtt
ottiene l'attributo ARN della risorsa AWS::SQS::Queue myqueue
.
Il documento di policy giveaccesstotopiconly
viene aggiunto all'utente per consentirgli di eseguire tutte le operazioni di Amazon SNS nella risorsa dell'argomento Amazon SNS mytopic
e rifiutare l'accesso a tutte le altre risorse della coda Amazon SNS. La funzione Ref
ottiene l'attributo ARN della risorsa AWS::SNS::Topic mytopic
.
JSON
"myuser" : { "Type" : "AWS::IAM::User", "Properties" : { "Path" : "/", "LoginProfile" : { "Password" : "myP@ssW0rd" }, "Policies" : [ { "PolicyName" : "giveaccesstoqueueonly", "PolicyDocument" : { "Version": "2012-10-17", "Statement" : [ { "Effect" : "Allow", "Action" : [ "sqs:*" ], "Resource" : [ { "Fn::GetAtt" : [ "myqueue", "Arn" ] } ] }, { "Effect" : "Deny", "Action" : [ "sqs:*" ], "NotResource" : [ { "Fn::GetAtt" : [ "myqueue", "Arn" ] } ] } ] } }, { "PolicyName" : "giveaccesstotopiconly", "PolicyDocument" : { "Version": "2012-10-17", "Statement" : [ { "Effect" : "Allow", "Action" : [ "sns:*" ], "Resource" : [ { "Ref" : "mytopic" } ] }, { "Effect" : "Deny", "Action" : [ "sns:*" ], "NotResource" : [ { "Ref" : "mytopic" } ] } ] } } ] } }
YAML
myuser: Type: AWS::IAM::User Properties: Path: "/" LoginProfile: Password: myP@ssW0rd Policies: - PolicyName: giveaccesstoqueueonly PolicyDocument: Version: '2012-10-17' Statement: - Effect: Allow Action: - sqs:* Resource: - !GetAtt myqueue.Arn - Effect: Deny Action: - sqs:* NotResource: - !GetAtt myqueue.Arn - PolicyName: giveaccesstotopiconly PolicyDocument: Version: '2012-10-17' Statement: - Effect: Allow Action: - sns:* Resource: - !Ref mytopic - Effect: Deny Action: - sns:* NotResource: - !Ref mytopic
Dichiarazione di una risorsa chiave di accesso IAM
Questo frammento mostra una risorsa AWS::IAM::AccessKey. La risorsa myaccesskey
crea una chiave di accesso e la assegna a un utente IAM dichiarato come risorsa AWS::IAM::User nel modello.
JSON
"myaccesskey" : { "Type" : "AWS::IAM::AccessKey", "Properties" : { "UserName" : { "Ref" : "myuser" } } }
YAML
myaccesskey: Type: AWS::IAM::AccessKey Properties: UserName: !Ref myuser
Puoi ottenere la chiave segreta per una risorsa AWS::IAM::AccessKey
utilizzando la funzione Fn::GetAtt
. Uno dei modi per recuperare la chiave segreta è inserirla in un valore Output
. Puoi ottenere la chiave di accesso utilizzando la funzione Ref
. Le seguenti dichiarazioni del valore Output
ottengono la chiave di accesso e la chiave segreta per myaccesskey
.
JSON
"AccessKeyformyaccesskey" : { "Value" : { "Ref" : "myaccesskey" } }, "SecretKeyformyaccesskey" : { "Value" : { "Fn::GetAtt" : [ "myaccesskey", "SecretAccessKey" ] } }
YAML
AccessKeyformyaccesskey: Value: !Ref myaccesskey SecretKeyformyaccesskey: Value: !GetAtt myaccesskey.SecretAccessKey
Puoi anche passare la chiave di AWS accesso e la chiave segreta a un' EC2 istanza Amazon o a un gruppo di Auto Scaling definito nel modello. La seguente dichiarazione AWS::EC2::Instance utilizza la proprietà UserData
per passare la chiave di accesso e la chiave segreta per la risorsa myaccesskey
.
JSON
"myinstance" : { "Type" : "AWS::EC2::Instance", "Properties" : { "AvailabilityZone" : "us-east-1a", "ImageId" : "ami-0ff8a91507f77f867", "UserData" : { "Fn::Base64" : { "Fn::Join" : [ "", [ "ACCESS_KEY=", { "Ref" : "myaccesskey" }, "&", "SECRET_KEY=", { "Fn::GetAtt" : [ "myaccesskey", "SecretAccessKey" ] } ] ] } } } }
YAML
myinstance: Type: AWS::EC2::Instance Properties: AvailabilityZone: "us-east-1a" ImageId: ami-0ff8a91507f77f867 UserData: Fn::Base64: !Sub "ACCESS_KEY=${myaccesskey}&SECRET_KEY=${myaccesskey.SecretAccessKey}"
Dichiarazione di una risorsa di gruppo IAM
Questo frammento mostra una risorsa AWS::IAM::Group. Il gruppo ha un percorso ("/myapplication/"
). Il documento di policy myapppolicy
viene aggiunto al gruppo per consentire agli utenti del gruppo di eseguire tutte le operazioni di Amazon SQS nella risorsa della coda Amazon SQS myqueue e rifiutare l'accesso a tutte le altre risorse Amazon SQS eccetto myqueue
.
Per assegnare una policy a una risorsa, IAM richiede l'ARN (Amazon Resource Name) per la risorsa. Nel frammento, la funzione Fn::GetAtt
ottiene l'ARN della coda della risorsa AWS::SQS::Queue.
JSON
"mygroup" : { "Type" : "AWS::IAM::Group", "Properties" : { "Path" : "/myapplication/", "Policies" : [ { "PolicyName" : "myapppolicy", "PolicyDocument" : { "Version": "2012-10-17", "Statement" : [ { "Effect" : "Allow", "Action" : [ "sqs:*" ], "Resource" : [ { "Fn::GetAtt" : [ "myqueue", "Arn" ] } ] }, { "Effect" : "Deny", "Action" : [ "sqs:*" ], "NotResource" : [ { "Fn::GetAtt" : [ "myqueue", "Arn" ] } ] } ] } } ] } }
YAML
mygroup: Type: AWS::IAM::Group Properties: Path: "/myapplication/" Policies: - PolicyName: myapppolicy PolicyDocument: Version: '2012-10-17' Statement: - Effect: Allow Action: - sqs:* Resource: !GetAtt myqueue.Arn - Effect: Deny Action: - sqs:* NotResource: !GetAtt myqueue.Arn
Aggiunta di utenti a un gruppo
La risorsa AWS::IAM::UserToGroupAddition aggiunge utenti a un gruppo. Nel seguente frammento, la risorsa addUserToGroup
aggiunge i seguenti utenti a un gruppo esistente denominato myexistinggroup2
: l'utente esistente existinguser1
e l'utente myuser
dichiarato come risorsa AWS::IAM::User nel modello.
JSON
"addUserToGroup" : { "Type" : "AWS::IAM::UserToGroupAddition", "Properties" : { "GroupName" : "myexistinggroup2", "Users" : [ "existinguser1", { "Ref" : "myuser" } ] } }
YAML
addUserToGroup: Type: AWS::IAM::UserToGroupAddition Properties: GroupName: myexistinggroup2 Users: - existinguser1 - !Ref myuser
Dichiarazione di una policy IAM
Questo frammento mostra come creare una policy e applicarla a più gruppi utilizzando una risorsa AWS::IAM::Policy denominata mypolicy
. La risorsa mypolicy
include una proprietà PolicyDocument
che consente le operazioni GetObject
, PutObject
e PutObjectAcl
sugli oggetti nel bucket S3 rappresentato dall'ARN arn:aws:s3:::myAWSBucket
. La risorsa mypolicy
applica la policy a un gruppo esistente denominato myexistinggroup1
e a un gruppo mygroup
che viene dichiarato nel modello come una risorsa AWS::IAM::Group. Questo esempio mostra come applicare una policy a un gruppo utilizzando la proprietà Groups
; tuttavia, è anche possibile utilizzare la proprietà Users
per aggiungere un documento della policy a un elenco di utenti.
JSON
"mypolicy" : { "Type" : "AWS::IAM::Policy", "Properties" : { "PolicyName" : "mygrouppolicy", "PolicyDocument" : { "Version": "2012-10-17", "Statement" : [ { "Effect" : "Allow", "Action" : [ "s3:GetObject" , "s3:PutObject" , "s3:PutObjectAcl" ], "Resource" : "arn:aws:s3:::myAWSBucket/*" } ] }, "Groups" : [ "myexistinggroup1", { "Ref" : "mygroup" } ] } }
YAML
mypolicy: Type: AWS::IAM::Policy Properties: PolicyName: mygrouppolicy PolicyDocument: Version: '2012-10-17' Statement: - Effect: Allow Action: - s3:GetObject - s3:PutObject - s3:PutObjectAcl Resource: arn:aws:s3:::myAWSBucket/* Groups: - myexistinggroup1 - !Ref mygroup
Dichiarazione di una policy di un bucket Amazon S3
Questo frammento mostra come creare una policy e applicarla a un bucket Amazon S3 utilizzando la risorsa AWS::S3::BucketPolicy. La risorsa mybucketpolicy
dichiara un documento di policy che consente all'utente IAM user1
di eseguire l'operazione GetObject
su tutti gli oggetti nel bucket S3 a cui la policy viene applicata. Nel frammento, la funzione Fn::GetAtt
ottiene l'ARN della risorsa user1
. La risorsa mybucketpolicy
applica la policy alla risorsa AWS::S3::BucketPolicy
mybucket. La funzione Ref
ottiene il nome del bucket della risorsa mybucket
.
JSON
"mybucketpolicy" : { "Type" : "AWS::S3::BucketPolicy", "Properties" : { "PolicyDocument" : { "Id" : "MyPolicy", "Version": "2012-10-17", "Statement" : [ { "Sid" : "ReadAccess", "Action" : [ "s3:GetObject" ], "Effect" : "Allow", "Resource" : { "Fn::Join" : [ "", [ "arn:aws:s3:::", { "Ref" : "mybucket" } , "/*" ] ] }, "Principal" : { "AWS" : { "Fn::GetAtt" : [ "user1", "Arn" ] } } } ] }, "Bucket" : { "Ref" : "mybucket" } } }
YAML
mybucketpolicy: Type: AWS::S3::BucketPolicy Properties: PolicyDocument: Id: MyPolicy Version: '2012-10-17' Statement: - Sid: ReadAccess Action: - s3:GetObject Effect: Allow Resource: !Sub "arn:aws:s3:::${mybucket}/*" Principal: AWS: !GetAtt user1.Arn Bucket: !Ref mybucket
Dichiarazione di una policy di un argomento Amazon SNS
Questo frammento mostra come creare una policy e applicarla a un argomento Amazon SNS utilizzando la risorsa AWS::SNS::TopicPolicy. La risorsa mysnspolicy
include una proprietà PolicyDocument
che consente alla risorsa AWS::IAM::User myuser
di eseguire l'operazione Publish
su una risorsa AWS::SNS::Topic mytopic
. Nel frammento, la funzione Fn::GetAtt
ottiene l'ARN per la risorsa myuser
e la funzione Ref
ottiene l'ARN per la risorsa mytopic
.
JSON
"mysnspolicy" : { "Type" : "AWS::SNS::TopicPolicy", "Properties" : { "PolicyDocument" : { "Id" : "MyTopicPolicy", "Version" : "2012-10-17", "Statement" : [ { "Sid" : "My-statement-id", "Effect" : "Allow", "Principal" : { "AWS" : { "Fn::GetAtt" : [ "myuser", "Arn" ] } }, "Action" : "sns:Publish", "Resource" : "*" } ] }, "Topics" : [ { "Ref" : "mytopic" } ] } }
YAML
mysnspolicy: Type: AWS::SNS::TopicPolicy Properties: PolicyDocument: Id: MyTopicPolicy Version: '2012-10-17' Statement: - Sid: My-statement-id Effect: Allow Principal: AWS: !GetAtt myuser.Arn Action: sns:Publish Resource: "*" Topics: - !Ref mytopic
Dichiarazione di una policy Amazon SQS
Questo frammento mostra come creare una policy e applicarla a una coda Amazon SQS utilizzando la risorsa AWS::SQS::QueuePolicy. La proprietà PolicyDocument
consente all'utente esistente myapp
(specificato dal relativo ARN) di eseguire l'operazione SendMessage
su una coda esistente, specificata tramite il relativo URL e su una risorsa AWS::SQS::Queue myqueue. La funzione Ref ottiene l'URL per la risorsa myqueue
.
JSON
"mysqspolicy" : { "Type" : "AWS::SQS::QueuePolicy", "Properties" : { "PolicyDocument" : { "Id" : "MyQueuePolicy", "Version" : "2012-10-17", "Statement" : [ { "Sid" : "Allow-User-SendMessage", "Effect" : "Allow", "Principal" : { "AWS" : "arn:aws:iam::123456789012:user/myapp" }, "Action" : [ "sqs:SendMessage" ], "Resource" : "*" } ] }, "Queues" : [ "https://sqs.us-east-2
aws-region
.amazonaws.com/123456789012/myexistingqueue", { "Ref" : "myqueue" } ] } }
YAML
mysqspolicy: Type: AWS::SQS::QueuePolicy Properties: PolicyDocument: Id: MyQueuePolicy Version: '2012-10-17' Statement: - Sid: Allow-User-SendMessage Effect: Allow Principal: AWS: arn:aws:iam::123456789012:user/myapp Action: - sqs:SendMessage Resource: "*" Queues: - https://sqs.
aws-region
.amazonaws.com/123456789012/myexistingqueue - !Ref myqueue
Esempi di modello per i ruoli IAM
Questa sezione fornisce esempi di CloudFormation modelli per i ruoli IAM per le EC2 istanze.
Per ulteriori informazioni, consulta i ruoli IAM per Amazon EC2 nella Amazon EC2 User Guide.
Ruolo IAM con EC2
In questo esempio, il profilo dell'istanza è referenziato dalla IamInstanceProfile
proprietà dell' EC2 istanza. Sia la policy dell'istanza che del ruolo fanno riferimento a AWS::IAM::Role.
JSON
{ "AWSTemplateFormatVersion": "2010-09-09", "Resources": { "myEC2Instance": { "Type": "AWS::EC2::Instance", "Version": "2009-05-15", "Properties": { "ImageId": "ami-0ff8a91507f77f867", "InstanceType": "m1.small", "Monitoring": "true", "DisableApiTermination": "false", "IamInstanceProfile": { "Ref": "RootInstanceProfile" } } }, "RootRole": { "Type": "AWS::IAM::Role", "Properties": { "AssumeRolePolicyDocument": { "Version" : "2012-10-17", "Statement": [ { "Effect": "Allow", "Principal": { "Service": [ "ec2.amazonaws.com" ] }, "Action": [ "sts:AssumeRole" ] } ] }, "Path": "/" } }, "RolePolicies": { "Type": "AWS::IAM::Policy", "Properties": { "PolicyName": "root", "PolicyDocument": { "Version" : "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": "*", "Resource": "*" } ] }, "Roles": [ { "Ref": "RootRole" } ] } }, "RootInstanceProfile": { "Type": "AWS::IAM::InstanceProfile", "Properties": { "Path": "/", "Roles": [ { "Ref": "RootRole" } ] } } } }
YAML
AWSTemplateFormatVersion: '2010-09-09' Resources: myEC2Instance: Type: AWS::EC2::Instance Version: '2009-05-15' Properties: ImageId: ami-0ff8a91507f77f867 InstanceType: m1.small Monitoring: 'true' DisableApiTermination: 'false' IamInstanceProfile: !Ref RootInstanceProfile RootRole: Type: AWS::IAM::Role Properties: AssumeRolePolicyDocument: Version: '2012-10-17' Statement: - Effect: Allow Principal: Service: - ec2.amazonaws.com Action: - sts:AssumeRole Path: "/" RolePolicies: Type: AWS::IAM::Policy Properties: PolicyName: root PolicyDocument: Version: '2012-10-17' Statement: - Effect: Allow Action: "*" Resource: "*" Roles: - !Ref RootRole RootInstanceProfile: Type: AWS::IAM::InstanceProfile Properties: Path: "/" Roles: - !Ref RootRole
Ruolo IAM con il gruppo Auto Scaling
In questo esempio, il profilo dell'istanza è referenziato dalla IamInstanceProfile
proprietà di una configurazione di avvio di Amazon EC2 Auto Scaling.
JSON
{ "AWSTemplateFormatVersion": "2010-09-09", "Resources": { "myLCOne": { "Type": "AWS::AutoScaling::LaunchConfiguration", "Version": "2009-05-15", "Properties": { "ImageId": "ami-0ff8a91507f77f867", "InstanceType": "m1.small", "InstanceMonitoring": "true", "IamInstanceProfile": { "Ref": "RootInstanceProfile" } } }, "myASGrpOne": { "Type": "AWS::AutoScaling::AutoScalingGroup", "Version": "2009-05-15", "Properties": { "AvailabilityZones": [ "us-east-1a" ], "LaunchConfigurationName": { "Ref": "myLCOne" }, "MinSize": "0", "MaxSize": "0", "HealthCheckType": "EC2", "HealthCheckGracePeriod": "120" } }, "RootRole": { "Type": "AWS::IAM::Role", "Properties": { "AssumeRolePolicyDocument": { "Version" : "2012-10-17", "Statement": [ { "Effect": "Allow", "Principal": { "Service": [ "ec2.amazonaws.com" ] }, "Action": [ "sts:AssumeRole" ] } ] }, "Path": "/" } }, "RolePolicies": { "Type": "AWS::IAM::Policy", "Properties": { "PolicyName": "root", "PolicyDocument": { "Version" : "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": "*", "Resource": "*" } ] }, "Roles": [ { "Ref": "RootRole" } ] } }, "RootInstanceProfile": { "Type": "AWS::IAM::InstanceProfile", "Properties": { "Path": "/", "Roles": [ { "Ref": "RootRole" } ] } } } }
YAML
AWSTemplateFormatVersion: '2010-09-09' Resources: myLCOne: Type: AWS::AutoScaling::LaunchConfiguration Version: '2009-05-15' Properties: ImageId: ami-0ff8a91507f77f867 InstanceType: m1.small InstanceMonitoring: 'true' IamInstanceProfile: !Ref RootInstanceProfile myASGrpOne: Type: AWS::AutoScaling::AutoScalingGroup Version: '2009-05-15' Properties: AvailabilityZones: - "us-east-1a" LaunchConfigurationName: !Ref myLCOne MinSize: '0' MaxSize: '0' HealthCheckType: EC2 HealthCheckGracePeriod: '120' RootRole: Type: AWS::IAM::Role Properties: AssumeRolePolicyDocument: Version: '2012-10-17' Statement: - Effect: Allow Principal: Service: - ec2.amazonaws.com Action: - sts:AssumeRole Path: "/" RolePolicies: Type: AWS::IAM::Policy Properties: PolicyName: root PolicyDocument: Version: '2012-10-17' Statement: - Effect: Allow Action: "*" Resource: "*" Roles: - !Ref RootRole RootInstanceProfile: Type: AWS::IAM::InstanceProfile Properties: Path: "/" Roles: - !Ref RootRole