Per creare una macchina a stati utilizzerai la console AWS Step Functions. Step Functions è un servizio Web che consente di coordinare i componenti di applicazioni e microservizi distribuiti tramite flussi di lavoro visivi. Le funzioni Lambda vengono invocate quando la macchina a stati viene eseguita e raggiunge lo stato Task.
Ai fini di questo progetto, la macchina a stati esegue le seguenti attività:
- Crea un generatore di immagini AppStream 2.0.
- Ottiene lo stato del generatore di immagini e della flotta di AppStream 2.0.
- Invia un URL di streaming.
- Ottiene il nome dell'immagine AppStream 2.0.
- Crea una flotta e uno stack AppStream e associa la flotta a uno stack.
- Arresta la flotta e il generatore di immagini di AppStream 2.0.
Per creare la macchina a stati, procedi come segue:
- Apri la console AWS Step Functions all'indirizzo https://console.aws.amazon.com/states/.
- Esegui una delle seguenti operazioni:
- Se non hai ancora creato funzioni di macchine a stati, verrà visualizzata la pagina Getting Started (Nozioni di base). Scegliere Getting Started (Nozioni di base) e quindi scegliere State Machines (Macchine a stati).
- Se hai già creato una funzione di macchine a stati, nell'angolo in alto a destra della pagina State machines (Macchine a stati) scegliere Create state machine (Crea macchina a stati).
- Nella pagina Define State Machine (Definisci macchina a stati), mantenere selezionato Author from code snippet (Crea da frammenti di codice).
- Nella sezione Type (Tipo), mantenere selezionato Standard.
- Nella sezione State machine definition (Definizione della macchina a stati), eliminare il codice segnaposto e incollare il seguente codice nella finestra di modifica.
{
"StartAt": "Create Image Builder",
"States": {
"Create Image Builder": {
"Type": "Task",
"Resource":
"arn:aws:lambda:<REGION-CODE>:<AWS-ACCOUNT-ID>:function:examplecorp_eao_createimagebuilder",
"ResultPath": "$.type",
"Next": "Resource Created?"
},
"Resource Created?": {
"Type": "Choice",
"Choices": [
{
"Variable": "$.type",
"NumericEquals": -1,
"Next": "Creation Failed"
},
{
"Variable": "$.type",
"NumericEquals": 1,
"Next": "Sleep"
},
{
"Variable": "$.type",
"NumericEquals": 2,
"Next": "Sleep"
}
],
"Default": "Sleep"
},
"Sleep": {
"Type": "Wait",
"Seconds": 300,
"Next": "Get Status"
},
"Get Status": {
"Type": "Task",
"Resource":
"arn:aws:lambda:<REGION-CODE>:<AWS-ACCOUNT-ID>:function:examplecorp_eao_getstatus",
"Next": "Instance Status",
"ResultPath": "$.status"
},
"Instance Status": {
"Type": "Choice",
"Choices": [
{
"Variable": "$.status",
"NumericEquals": -1,
"Next": "Creation Failed"
},
{
"Variable": "$.status",
"NumericEquals": 1,
"Next": "Send StreamingURL"
}
],
"Default": "Sleep"
},
"Send StreamingURL": {
"Type": "Task",
"Resource":
"arn:aws:lambda:<REGION-CODE>:<AWS-ACCOUNT-ID>:function:examplecorp_eao_sendstreamingurl",
"Next": "Split",
"ResultPath": "$.status"
},
"Split": {
"Type": "Choice",
"Choices": [
{
"Variable": "$.type",
"NumericEquals": 1,
"Next": "Wait For Snapshot"
},
{
"Variable": "$.type",
"NumericEquals": 2,
"Next": "1 Hour Timer"
}
],
"Default": "1 Hour Timer"
},
"Wait For Snapshot": {
"Type": "Wait",
"Seconds": 1500,
"Next": "IB Status"
},
"IB Status": {
"Type": "Task",
"Resource":"arn:aws:lambda:<REGION-CODE>:<AWS-ACCOUNT-ID>:function:examplecorp_eao_getstatus",
"ResultPath": "$.status",
"Next": "Snapshotting?"
},
"Snapshotting?": {
"Type": "Choice",
"Choices": [
{
"Variable": "$.status",
"NumericEquals": 2,
"Next": "Get ImageName"
},
{
"Variable": "$.status",
"NumericEquals": 3,
"Next": "Get ImageName"
}
],
"Default": "Wait For Snapshot"
},
"Get ImageName": {
"Type": "Task",
"Resource":"arn:aws:lambda:<REGION-CODE>:<AWS-ACCOUNT-ID>:function:examplecorp_eao_getimagename",
"ResultPath": "$.imagename",
"Next": "Wait For ImageReady"
},
"Wait For ImageReady": {
"Type": "Wait",
"Seconds": 300,
"Next": "Image Status"
},
"Image Status": {
"Type": "Task",
"Resource":"arn:aws:lambda:<REGION-CODE>:<AWS-ACCOUNT-ID>:function:examplecorp_eao_getstatus",
"ResultPath": "$.status",
"Next": "ImageReady?"
},
"ImageReady?": {
"Type": "Choice",
"Choices": [
{
"Variable": "$.status",
"NumericEquals": 3,
"Next": "Create Fleet"
}
],
"Default": "Wait For ImageReady"
},
"Create Fleet": {
"Type": "Task",
"Resource":"arn:aws:lambda:<REGION-CODE>:<AWS-ACCOUNT-ID>:function:examplecorp_eao_createfleet",
"ResultPath": "$.type",
"Next": "Resource Created?"
},
"1 Hour Timer": {
"Type": "Wait",
"Seconds": 3600,
"Next": "StopResources"
},
"Creation Failed": {
"Type": "Fail",
"Cause": "Process Failed",
"Error": "Process Failed"
},
"StopResources": {
"Type": "Task",
"Resource":
"arn:aws:lambda:<REGION-CODE>:<AWS-ACCOUNT-ID>:function:examplecorp_eao_stopresources",
"End": true
}
}
}
6. Sostituisci quanto segue con i tuoi valori:
• Sostituire <AWS-ACCOUNT-ID> con il numero del tuo account AWS.
• Sostituire <REGION-CODE> con la regione AWS che hai scelto. Il valore deve contenere solo lettere minuscole.
7. Scegliere
Next (Avanti).
8. Nel campo
Name (Nome), digitare
examplecorp_eao.
9. Nella sezione
Permissions (Autorizzazioni), selezionare
Choose an existing role (Scegli un ruolo esistente).
10. Nel menu a discesa
Existing roles (Ruoli esistenti) selezionare
examplecorp_eao_role, il ruolo IAM che hai creato nel Modulo 2.
11. Selezionare
Create state machine (Crea macchina a stati).
12. Nella parte superiore della pagina, nella sezione
Details (Dettagli), viene visualizzato l'
ARN della macchina a stati. Prendi nota dell'ARN. Questo ARN verrà richiesto più avanti nel progetto ed è formattato come arn:aws:states:<REGION-CODE>:<AWS_ACCOUNT-ID>:stateMachine:<STATE-MACHINE-NAME>